[英]Is this how to iterate through documents on a Firestore collection in flutter?
In the code below, I am attempting to retrieve all documents within a collection in firestore using flutter and dart.在下面的代码中,我尝试使用 flutter 和 dart 检索 Firestore 中的集合中的所有文档。 Is this the best way to do it?这是最好的方法吗?
Query () async {
QuerySnapshot snapshot = await Firestore.instance.collection("collectionName").getDocuments();
snapshot.documents.forEach((document){
if(document.exists){
print('Documents exist');
}
else {
print('document does not exist');
}
});
Yes, this is the way to retrieve the documents from a collection.是的,这是从集合中检索文档的方法。 Moreover, by default firestore will try to cache data offline and load it from there which sometimes prevents it get the data from server even if the server has a different data.此外,默认情况下,firestore 会尝试离线缓存数据并从那里加载,这有时会阻止它从服务器获取数据,即使服务器有不同的数据。 To prevent that you can add source flag inside getDocuments method, .getDocuments(source: Source.server);
为了防止您可以在 getDocuments 方法中添加源标志, .getDocuments(source: Source.server);
like this.像这样。 And if you want to architect your application in different ways then you can do it but, you will still have to call this same method to retrieve data from firestore如果您想以不同的方式构建您的应用程序,那么您可以这样做,但是您仍然必须调用相同的方法来从 firestore 检索数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.