Kennedy Owusu
Kennedy Owusu

Reputation: 6070

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');
    }
  });

Upvotes: 0

Views: 586

Answers (1)

Saiful Islam Adar
Saiful Islam Adar

Reputation: 221

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

Upvotes: 2

Related Questions