Extracting info from QuerySnapshot variable in flutter app

Viewed 3285

This code is running fine with futurebuilder and i m getting a listview properly. But i want to see into the documents n print the details in console. I m not getting any idea about how to do this with QuerySnapshot variable.

Future getP() async {
  var firestore = Firestore.instance;
  var q = await firestore.collection('place_list').getDocuments();
  print(q.documents);
  return q.documents;
}

I think I have to call it n wait for the responses then print them, can anyone guide me how to do it?

This is the co

This is what i am getting when i try to print the values to console

3 Answers
 List<Map<String, dynamic>> list = 
  q.documents.map((DocumentSnapshot doc){
  return doc.data;
}).toList();

print(list);

Though the answer is right the current firebase API has changed drastically now to access QuerySnapshot one can follow the below code.

FirebaseFirestore.instance
.collection('users')
.get()
.then((QuerySnapshot querySnapshot) => {
    querySnapshot.docs.forEach((doc) {
        print(doc["first_name"]);
    });
});

And if you are using async/await then first you need to resolve the AsyncSnapshot and then work on it. If you like:

    return FutureBuilder(
    future: PropertyService(uid:userId).getUserProperties(),
    builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot){
      if (snapshot.hasError) {
        return Text("Something went wrong");
      }

      if (snapshot.connectionState == ConnectionState.done) {

        snapshot.data.docs.forEach((element) {
          Property property = Property.fromJson(element.data());
        });
        return Text("Demo Text");

      }

      return LoadingPage();
    }
);

taken from url

//But I am not getting all the documents present in my firestore DB collection. The first 10 or so entries are getting printed in the console. //

I think that is standard behavior. If you have one million records it can't print everything in console. To check any particular set of documents you have to filter through where condition in query.

Related