firebase - Flutter & Firestore:如何首先获得最新的?
问题描述
如何从 Firestore 获取最新的第一个?
FirebaseFirestore.instance.collection(Strings.factsText)
.where(
Strings.category,
whereIn: [Strings.climateChange])
.orderBy('timestamp', descending: true)
.snapshots(),
...
final factsDocs = snapshot.data.documents;
return FactsListContainer(factsDocs: factsDocs);
});
.where
使用 with 时,问题似乎出在with .orderBy
!
解决方案
如果您有一个createdAt
带有时间戳的字段,或者一个始终递增的值,您可以按该字段的降序获取快照:
stream: FirebaseFirestore.instance
.collection(Strings.factsText)
.orderBy('timestamp', descending: true)
.where(
Strings.category,
whereIn: [Strings.climateChange])
.snapshots(),
Query.orderBy
另请参阅有关方法的 FlutterFire 文档。
推荐阅读
- go - []fs.FileInfo 不能作为接受自定义接口的函数参数传入
- c - 你能指出这个菜鸟 C 代码中尝试创建链表的错误吗
- javascript - Discord.js v12 角色查询
- javascript - Javascript - 关于设置超时的问题
- java - 如何将输入限制为仅 1 位
- node.js - Angular 应用程序在本地工作,但在 Heroku 上没有部署失败
- laravel - 提交表单时如何使用 htmlspecialchars_decode 和 stripslashes 函数清除数据?
- flutter - 如何在 Flutter 中解决这个问题,我可以在图像中但作为文件,然后将图像分配给我的图像容器?
- reactjs - Concat Dates Array 正在更改数组最后一个索引中的日期
- pandas - 带有多索引熊猫数据框的条形图