gpt4 book ai didi

flutter - 未按预期从 await 函数返回 Future

转载 作者:IT王子 更新时间:2023-10-29 07:18:52 25 4
gpt4 key购买 nike

函数signInWithGoogle调用 getUser函数从 Firestore 数据库中检索用户信息并期望一个 User作为返回。因为这是一个 Firestore API 调用,所以 UsergetUser 返回是 FuturegetUserawait 调用.

getUser功能,User按预期填充并使用 debugPrint("new user schedule 0: " + i[0].toJson()); 验证

然而在signInWithGoogle功能,User未收到来自 Future<User> 的回复,如对 schedule 的空引用所示在执行 debugPrint("user schedule 0: " + u.schedule[0].toJson());

我尝试过以多种方式设置返回值(getUser 函数),包括在设置它的值并返回之前单独实例化一个用户类。

Future<User> getUser(_uid) async {
DocumentSnapshot qs = await Firestore.instance
.collection('users')
.document(_uid)
.get();
if (qs.exists) {
setState(() {
state.loadingStatus = "Getting User Information";
});
return new User(
schedule: await getRecipes(
Firestore.instance
.collection('users')
.document(_uid)
.collection('schedule')
).then((i) {
debugPrint("get schedule");
debugPrint("new user schedule 0: " + i[0].toJson());
}).catchError((error) {
debugPrint('Error: $error');
}),
favorites: await getRecipes(
Firestore.instance
.collection('users')
.document(_uid)
.collection('favorites')
).then((i) {debugPrint("get favorites");}).catchError((error) {
debugPrint('Error: $error');
}),
subscription: await getSubscription(_uid).then((i) {debugPrint("get subscription");}),
pantry: await getIngredientsList(
Firestore.instance
.collection('users')
.document(_uid)
.collection('pantry')
).then((i) {debugPrint("get pantry");}).catchError((error) {
debugPrint('Error: $error');
}),
shopping: await getIngredientsList(
Firestore.instance
.collection('users')
.document(_uid)
.collection('shopping')
).then((i) {debugPrint("get shopping list");}).catchError((error) {
debugPrint('Error: $error');
}),
preferences: await getPreferences(_uid).then((i) {debugPrint("get preferences");}),
);
}else {
setState(() {
state.loadingStatus = "Creating new User Information";
});
return User.newUser();
}
}

Future<Null> signInWithGoogle() async {
setState(() {
state.loadingStatus = "Signing in with Google";
});
if (googleAccount == null) {
// Start the sign-in process:
googleAccount = await googleSignIn.signIn();
}
FirebaseUser firebaseUser = await signIntoFirebase(googleAccount);
User user = await getUser(firebaseUser.uid);
debugPrint("user schedule 0: " + user.schedule[0].toJson());
setState(() {
state.isLoading = false;
state.loadingStatus = "";
state.user = firebaseUser;
state.userInfo = user;
});
}
I/flutter (17962): new user schedule 0: {Proper data is printed here...
I/flutter (17962): get favorites
I/flutter (17962): get subscription
I/flutter (17962): get pantry
I/flutter (17962): get shopping list
I/flutter (17962): get preferences
E/flutter (17962): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter (17962): Receiver: null
E/flutter (17962): Tried calling: [](0)

我希望在打印 schedule[0] 时不会收到空引用错误来自 signInWithGoogle当同样的调用打印schedule[0]getUser 工作功能。

这可能是我所缺少的愚蠢简单的东西,但在查看过去 3 小时的代码后我无法弄清楚发生了什么。

如果您需要任何进一步的信息,请告诉我。

最佳答案

问题就在这里:

schedule: await getRecipes(
Firestore.instance
.collection('users')
.document(_uid)
.collection('schedule')
).then((i) {
debugPrint("get schedule");
debugPrint("new user schedule 0: " + i[0].toJson());

// this is a problem, there is no return!

}).catchError((error) {
debugPrint('Error: $error');
}),

这将 null 传递给 schedule: 参数,这不是您的意图。当您await someFuture.then(something) 时,您从something 中获取返回值,它是null在这种情况下,不是 someFuture 的解析值。

这是我们recommend not to mix async/await with .then的原因之一

关于flutter - 未按预期从 await 函数返回 Future,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56410525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com