5

How do I return a QuerySnapshot as a Future >> ?

Code snippet:

Future <List<Map<dynamic, dynamic>>>() {
List<Map<dynamic,dynamic>> list;
.....

.....
QuerySnapshot collectionSnapshot = await collectionRef.getDocuments();

list = collectionSnapshot.documents;  <--- ERROR
return list;

}

I think I need to use a Map of but couldn't get around it to work.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
LiveRock
  • 1,419
  • 2
  • 17
  • 27

2 Answers2

12

collectionSnapshot.documents return List and not List types, you will need to convert List of documentsnapshots to List<Map<dynamic, dynamic>>. My be something like this:

Future <List<Map<dynamic, dynamic>>> getCollection() async{
List<DocumentSnapshot> templist;
List<Map<dynamic, dynamic>> list = new List();
CollectionReference collectionRef = Firestore.instance.collection("path");
QuerySnapshot collectionSnapshot = await collectionRef.getDocuments();

templist = collectionSnapshot.documents; // <--- ERROR

list = templist.map((DocumentSnapshot docSnapshot){
  return docSnapshot.data;
}).toList();

return list;
}  
Ganapat
  • 6,984
  • 3
  • 24
  • 38
0

Ganapat's answer worked out for me with a few minor changes.

Future <List<Map<dynamic, dynamic>>> getCollection() async{
List<DocumentSnapshot> templist;
List<Map<dynamic, dynamic>> list = new List();
CollectionReference collectionRef = Firestore.instance.collection("path");
QuerySnapshot collectionSnapshot = await collectionRef.get(); // <--- This method is now get().

templist = collectionSnapshot.documents; // <--- ERROR

list = templist.map((DocumentSnapshot docSnapshot){
  return docSnapshot.data() as Map<Dynamic,Dynamic>; // <--- Typecast this.
}).toList();

 

return list;
}  
DevSec Guy
  • 21
  • 2