[英]After get() of DocumentSnapshot, program is not executing next line in Flutter
Function1 and Function2 are not executed completely. Function1 和 Function2 未完全执行。
//Function1 //函数1
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 //Function2(从“users”集合的子集合中获取数据
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().当我单步执行时,在 getUserDetails() 本身中,它会在执行完.get() 之后返回到调用方法。 It is not even checking if (documentSnapshot.exists) condition.它甚至没有检查 (documentSnapshot.exists) 条件。 The same happens with fetchPunchinDetails() function. fetchPunchinDetails() function 也会发生同样的情况。 Please help..请帮忙..
fetchPunchinDetails
and getUserDetails
are future method, try using await
before theses to complete. fetchPunchinDetails
和getUserDetails
是未来的方法,在完成之前尝试使用await
。
_fetchAndGotoScreens() async{
if (loginSuccess = true) {
await getUserDetails();
await fetchPunchinDetails();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.