Issue
This Content is from Stack Overflow. Question asked by user19976096
Function1 and Function2 are not executed completely.
//Function1
Future<void> getUserDetails() async {
DocumentSnapshot documentSnapshot = await FirebaseFirestore.instance
.collection('users')
.doc('aLhON9H3H1ZpZvUB4ISUrK45Hk93')
.get();
if (documentSnapshot.exists) {
globalImagePath = documentSnapshot['imageURL'];
}
}
//Function2 (fetch data from subcollection of “users” collection
Future<void> fetchPunchinDetails() async {
try {
var result = await _users
.doc(user!.uid)
.collection('attendance')
.where('outTime', isEqualTo: null)
.get();
String dayStart = result.docs[0]['inTime'];
if (dayStart.isNotEmpty) {
dayStarted = true;
}
} catch (e) {
print(e.toString());
}
}
// Calling above two methods
_fetchAndGotoScreens() {
if (loginSuccess = true) {
getUserDetails(); //--------calling Function1
fetchPunchinDetails(); //----------calling Function2
//Go to attendance page-----------
Navigator.of(context)
.push(
MaterialPageRoute(
builder: (context) =>
Attendance(imagePath: imagePath, dayStartTime: dayStartTime),
),
)
.catchError((e) {
print(e);
});
} else {
// go to home page------------
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => MyHome()))
.catchError((e) {
print(e);
});
}
}
When I step through, in getUserDetails() itself, it returns to the calling method just after executing .get(). It is not even checking if (documentSnapshot.exists) condition. The same happens with fetchPunchinDetails() function. Please help..
Solution
fetchPunchinDetails
and getUserDetails
are future method, try using await
before theses to complete.
_fetchAndGotoScreens() async{
if (loginSuccess = true) {
await getUserDetails();
await fetchPunchinDetails();
This Question was asked in StackOverflow by user19976096 and Answered by Yeasin Sheikh It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.