Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Convert from QuerySnapshot to a Future <List<Map<dynamic, dynamic>>>

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.

like image 534
LiveRock Avatar asked Dec 13 '22 16:12

LiveRock


1 Answers

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;
}  
like image 97
Ganapat Avatar answered Jan 19 '23 00:01

Ganapat