'Flutter holding execution until future function complete (firestore query)
in my flutter app I need to retrieve some data from firestore database first, then continue executing the app, because the retrieve function gives the inputs to the widgets I would like to build.
I am calling the firestore future function -GetGlobalData()- from the init state, then I Would like to move to the next page only when the data is retrieved.
however, I discovred that the app moves to the second page before the function is totally completed which makes the widgets are built incorrectly.
I would like to prevent moving to the next page -push- until the function is completed. I am new to flutter, I guess it could be due to it is "Future" function? how could I solve this?
My init state code:
@override
void initState() {
super.initState();
isloading = false;
FirebaseAuth.instance.authStateChanges().listen((User? user) {
if (user == null) {
print('user is null');
Navigator.push(context,
MaterialPageRoute(builder: (context) => const SigninPage()));
}
if (user != null) {
globaluserid = user.uid;
GetGlobalData();
Navigator.push(context,
MaterialPageRoute(builder: (context) => const CategoriesGrid()));
}
});
}
and this is the GetGlobalData function:
Future GetGlobalData() async {
//CollectionReference userscollectionreference =
// FirebaseFirestore.instance.collection('UsersCollection');
DocumentSnapshot docsnapshot = await FirebaseFirestore.instance
.collection("UsersCollection")
.doc(globaluserid)
.get();
if (docsnapshot.exists) {
globaluserid = globaluserid;
globalusermobile = docsnapshot['User_mobile'].toString();
globalusername = docsnapshot['User_name'].toString();
globaluserdefaultcity = docsnapshot['User_defaultcity'].toString();
globalusergender = docsnapshot['User_gender'].toString();
globaluserstatus = docsnapshot['User_status'].toString();
globalusertype = docsnapshot['User_type'].toString();
globaluserlevel = docsnapshot['User_level'].toString();
print(globaluserid +
globalusermobile +
globalusername +
globaluserdefaultcity +
globalusergender +
globaluserstatus +
globalusertype +
globaluserlevel);
QuerySnapshot Querysnapshot1 = await FirebaseFirestore.instance
.collection("CitiesCollection")
.where('City_name', isEqualTo: globaluserdefaultcity)
.get();
final templist = [];
final templist2 = [];
for (var i = 1; i <= 9; i++) {
String j = i.toString();
if (Querysnapshot1.docs[0]['City_banner_image_$j'].toString() != "") {
templist
.add(Querysnapshot1.docs[0]['City_banner_image_$j'].toString());
}
if (Querysnapshot1.docs[0]['City_banner_id_$j'].toString() != "") {
templist2.add(Querysnapshot1.docs[0]['City_banner_id_$j'].toString());
}
}
selectedcitybannerImagelist = templist;
selectedcitybanneridlist = templist2;
print(templist);
}
}
Thanks
Edit: Adding the Solution
figured out that firestore only works with Async functions, so noway to get fixed data then complete execution. solution was to adapt my app to retrieve the data using strambuilder and use the retrieved data to build the widgets.
Solution 1:[1]
figured out that firestore only works with Async functions, so noway to get fixed data then complete execution. solution was to adapt my app to retrieve the data using strambuilder and use the retrieved data to build the widgets.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sherif Said Elahl |
