I am trying to fetch a field name "total" from mongodb and add all the total for a particular user as shown below
getCartTotal() async{
var total = 0;
Db db = new Db("mongodb://3.133.123.227/Kartofilldatabasetest");
await db.open();
var collection = db.collection('cart');
await collection.find({"customerId":phoneNumber}).forEach((v) {
total = total + v["total"];
});
db.close();
return total;
}
I am trying to use the returned value of total as follow in the init function i am calling it everytime the page is opened.
@override
void initState() {
// TODO: implement initState
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
await getTotal();
});
}
getTotal() async {
totalCartValue = await Mongocart(phoneNumber: widget.phoneNumber,description: '',image: '',price: '',quantity: '',total: '').getCartTotal();
}
@override
Widget build(BuildContext context) {
print(totalCartValue);
return Container();
}
}
Whenever i am trying to print the value it shows instance of future and not the value is printed. This is what is getting printed Instance of 'Future'
Below is my database screenshot
I want fetch the field total of all the object and assign it in the totalcartvalue currently it is printing 0.