I am having an issue where the QuerySnashot from the firestore giving correct value when printing in the android console but returns a null when building it on the flutter web app. Can someone please assist on what I am doing wrong? Here is the function giving me the correct print:
countpassengers() async {
String collection = "users";
QuerySnapshot _myDoc = await firebaseFiretore.collection(collection).get();
List<DocumentSnapshot> _myDocCount = _myDoc.docs;
print("Number of Users: :::::::::::::");
print(_myDocCount.length); // Count of Documen
totalusers = _myDocCount.length.toString() ;//ts in Collection
print (totalusers.toString());
return totalusers ;
}
and here is how I am calling it (returning null) somehow
InfoCard(
title: "Number of Passengers",
value: totalusers.toString(),//"3",
onTap: () {},
topColor: Colors.orange,
),
When you are returning the output from a function to State you need to rebuild the state by calling setState function.
By calling setState, the whole StatefulWidget knows something is updated and rebuild the state with new data. So insert setState after calling your Firestore function in the class. It could be inside a function or inside onTap: (){}
.
If you call that function inside your onTap: (){}
, don't forget to add async like that: onTap: () async{}
and await
your returning data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.