从Cloud Firestore获取所有用户的所有帖子 [英] Fetch all the posts of all the users from Cloud Firestore
问题描述
我正在构建 Flutter 应用.图中给出了Cloud Firestore数据库结构.我想使用 StreamBuilder
获得所有用户的所有帖子.我怎样才能做到这一点?到目前为止,我已经尝试过了:
I am building a Flutter app. Cloud firestore database structure is given in the picture. I want to get all the posts of all the users using a StreamBuilder
. How can I do that? So far I have tried this :
Stream<List<PostModel>> jobs() {
return usersCollection.snapshots().map((snapshot) {
return snapshot.documents.map((doc) {
doc['posts'].map((docu) {
return PostModel.fromSnapshot(docu);
});
}).toList();
});
}
推荐答案
如果您希望所有名为"posts"的子集合中的所有文档(针对所有用户),则可能需要//firebase.google.com/docs/firestore/query-data/queries#collection-group-query"rel =" nofollow noreferrer>收藏组查询.dev/documentation/cloud_firestore/latest/cloud_firestore/Firestore/collectionGroup.html"rel =" nofollow noreferrer> collectionGroup():
If you want all the document in all of the subcollections called "posts" (for all users), then you probably want a collection group query using collectionGroup():
db.collectionGroup("posts").snapshots()...
这将为您提供任何称为帖子"的集合或子集合中的所有文档,无论其嵌套在何处.
This will give you all documents in any collection or subcollection called "posts", no matter where it's nested.
这篇关于从Cloud Firestore获取所有用户的所有帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!