![](/img/trans.png)
[英]How to retrieve all documents from a collection in Firestore optimizing reads?
[英]Do cloud firestore snapshot reads all the documents in the collection?
我正在創建一個聊天屏幕。 我目前正在做的是使用 Streambuilder 來收聽“消息”集合並使用 ListView.builder() 顯示消息。 下面是我正在使用的代碼。
StreamBuilder<QuerySnapshot>(
stream: _fireStoreInstance
.collection('$collectionName/$docID/messages')
.orderBy('sentAt', descending: true)
.snapshots(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting)
return Center(
child: CircularProgressIndicator(),
);
List<Map> documents = snapshot.data.docs
.map((doc) => {'documentId': doc.id, ...doc.data()})
.toList();
return ListView.builder(
cacheExtent: MediaQuery.of(context).size.height,
reverse: true,
itemCount: documents.length,
padding:
const EdgeInsets.only(left: 15.0, right: 15.0, bottom: 5.0),
itemBuilder: (context, index) {
return MessageBubble(
...
);
},
);
},
),
我擔心的是,查詢會一次獲取集合中的所有文檔嗎? 如果是那么每次執行查詢都會有很多讀取
_fireStoreInstance
.collection('$collectionName/$docID/messages')
.orderBy('sentAt', descending: true)
.snapshots();
我需要使用限制來分頁嗎? 如果我分頁,我該如何收聽新消息? 謝謝您的幫助。
是的, .snapshots()
將讀取並繼續收聽所有符合查詢的文檔,如果您想要其中的一個子集,則必須使用.limit()
對其進行分頁。
我找到了這篇文章,其中包含有關如何使用無限滾動使用 Firestore 執行實時分頁的逐步視頻。 我認為這正是您要查找的內容,因此我不會發布任何代碼,因為您可以按照該示例進行操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.