Mrunal Joshi
Mrunal Joshi

Reputation: 377

Not able to retrieve documents from firestore

I am trying to retrieve the documents from firestore and store it into a list,but i am getting back [Instance of 'QueryDocumentSnapshot', Instance of 'QueryDocumentSnapshot'].Also,its not returning the list of documents but the fields as the length of array is only 2 but i have 6 documents inside "Org" Collection. I have asked similar question on SO but didn't get porper answer.

This is code i implemented to get the list of docuemnts:

Future<List> getHistory() async {
  List history;

  final List<DocumentSnapshot> documents = 
      (await FirebaseFirestore.instance.collection("Org").get()).docs;

  history = documents.map((documentSnapshot) => documentSnapshot).toList();

  return history;
}
@override
  initState() {
    emailInputController = new TextEditingController();
    pwdInputController = new TextEditingController();
    setlist();
    super.initState();
    Firebase.initializeApp();
    
  }
   void setlist()async {
     List list = await getHistory();
    print(list)
   }

I want to store all documents inside "Org" collection in a list.This is pic for reference. enter image description here

Upvotes: 0

Views: 160

Answers (1)

Christopher Moore
Christopher Moore

Reputation: 17143

You seem to be forgetting to do something in the line

history = documents.map((documentSnapshot) => documentSnapshot).toList();

Calling map here does nothing since you're not doing any mapping. You likely intended to call

history = documents.map((documentSnapshot) => documentSnapshot.data()).toList();

which retrieves the data from each snapshot and maps it to a new list.


If you want the document IDs instead of the fields, do .id instead of retrieving the data:

history = documents.map((documentSnapshot) => documentSnapshot.id).toList();

Upvotes: 2

Related Questions