Divyanshu Bhaskar
Divyanshu Bhaskar

Reputation: 345

Returning the list in the flutter receiving empty list

List<dynamic> allDataList = [];
 Future<void> compareUserBids() async {
final QuerySnapshot snapshot = await Firestore.instance
    .collection("Pro-$productName-$productId")
    .getDocuments();
List<DocumentSnapshot> templist;
templist = snapshot.documents;
allDataList = templist.map((DocumentSnapshot ds) {
  return {ds.documentID: ds.data};
}).toList();
 return allDataList;

}

@override
Widget build(BuildContext context) { 
  compareUserBids(); 
  print(allDataList);
}

When i am tying to print this data it is printing the null values but after hot reload it is printing the values

Upvotes: 1

Views: 708

Answers (1)

Manmohan Talwar
Manmohan Talwar

Reputation: 56

The reason you're getting an empty list is that you've not updated your widget after adding data into your global list. You should either add the data either inside setState or you can call setState once before returning your list.

 setState((){
   allDataList = templist.map((DocumentSnapshot ds) {
     return {ds.documentID: ds.data};
     },
    ).toList()
   },
 );

Upvotes: 4

Related Questions