简体   繁体   中英

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. 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. To prevent that you can add source flag inside getDocuments method, .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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM