[英]Get all documents from a Firestore collection in Flutter
I tried with different ways but i can't edit the structure of code我尝试了不同的方法,但我无法编辑代码结构
//First way
QuerySnapshot querySnapshot = await db.firestoreInstance.collection('user-history').get();
var list = querySnapshot.docs;
print('MY LIST ===== $list');
//Second way
final CollectionReference collectionRef = db.firestoreInstance
.collection(historyCollection);
print('MY SECOND LIST ===== $list');
collectionRef.get().then((qs) {
qs.docs.forEach((element) {
print('MY doc id ${element.id}');
});
});
In my firebase collection(historyCollection) i have four documents but the debugger returns me empty array [].在我的 firebase 集合(historyCollection)中,我有四个文档,但调试器返回空数组 []。 Is there another way to call all documents in certain collection through flutter?
还有另一种方法可以通过 flutter 调用某个集合中的所有文档吗? I'm trying to call this method through FutureBuilder component.
我正在尝试通过 FutureBuilder 组件调用此方法。
My version of firestore is: "cloud_firestore: ^0.16.0+1"我的firestore版本是:“cloud_firestore:^0.16.0+1”
The entire problem was not from these fragments of code.整个问题不在于这些代码片段。 This problem is came out from this that my collections have subcollections.
这个问题是因为我的 collections 有子集合。 I read about this and i understand that subcollections can live without their ancestors and the only way to access parents is to do this is directly specify the exact path and name of the document.
我读到了这个,我知道子集合可以在没有他们的祖先的情况下生存,访问父母的唯一方法是直接指定文档的确切路径和名称。 To work this code in my case was needed to add dummy components of my entire set of collections.
要在我的情况下使用此代码,需要添加我的整套 collections 的虚拟组件。 For more information please look up these two topics:
有关更多信息,请查看以下两个主题:
This should do the trick:这应该可以解决问题:
Future<List<dynamic>> getCollection(CollectionReference collection) async {
try {
QuerySnapshot snapshot = await collection.get();
List<dynamic> result = snapshot.docs.map((doc) => doc.data()).toList();
return result;
} catch (error) {
print(error);
return null;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.