firebase - Cloud Firestore 中的分页
问题描述
DocumentSnapshot
如果提供到startAt
或startAfter
不起作用,我如何对数据进行分页。
在网络上,您可以使用以下内容:
const first = db.collection('cities').orderBy('population').limit(25)
first.get().then(function (documentSnapshots) {
// Get the last visible document.
const lastVisible = documentSnapshots.docs[documentSnapshots.docs.length-1]
// Construct a new query starting at this document, get the next 25 cities.
const next = db.collection('cities').orderBy('population').startAfter(lastVisible).limit(25)
})
解决方案
由于 Cloud Firestore 删除了此功能,因此不再有解决方法,即您无法再使用orderBy(FieldPath.documentID())
。
您目前不能将DocumentSnapshot
用作参数。
但是,您可以简单地使用特定值。在此示例中,它将是文档中的population
数字lastVisible
:
final db = Firestore.instance;
db.collection('cities').orderBy('population').limit(25).getDocuments().then((querySnapshot) {
final lastVisible = snapshot.documents.last;
// Construct a new query starting at the last document, get the next 25 cities.
final next = db.collection('cities').orderBy('population')
.startAfter([lastVisible.data['population']]).limit(25);
});
如果您有具有相同值的字段,则可以使用以下逻辑按文档 ID 排序:
.orderBy(...).orderBy('__name__').startAfter([..., lastVisible.documentId])
推荐阅读
- php - 在 m1 mac 上安装旧的扩展 mongo php 驱动程序时找不到 stdlib.h
- amazon-web-services - 在 AWS 本地区域中创建 RDS 实例
- javascript - setRecordDataFor "Illegal set of identifier" 中的 EmberJS 数据断言
- python - Tkinter 网格“粘性”不适用于网格传播
- python - 如何将 tf.random 用于形状为 None 的张量
- c++ - 多字符常量警告
- python - 如何读取数组并返回数组?
- ios - 带有 InsetGroupedListStyle 的 SwiftUI 列表仅在滚动后更新正确的行高
- c# - C# - 如何过滤条件 A 的列表,然后仅针对这些匹配,按条件 B 过滤,而不破坏列表顺序
- c# - 从给定的一组数字构造到给定长度的所有可能数字的计数