【发布时间】:2021-01-28 11:35:57
【问题描述】:
我正在创建一个聊天屏幕。我目前正在做的是使用 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();
我需要使用 limit 进行分页吗?如果我分页如何收听新消息?感谢您的帮助。
【问题讨论】:
标签: firebase flutter google-cloud-firestore