在我的应用中,经过身份验证后,用户可以转到下一个屏幕。
signUpWithEmail().then((user) {
Navigator.push(context,
MaterialPageRoute(builder: (context) {
return HomePage();
}));
}).catchError((error) {
print("THE ERROR : $error");
});
现在 signUpWithEmail
可能会因各种原因而失败,例如:无效的电子邮件、互联网连接失败等。如何检测这些错误并阻止导航?这是 signUpWithEmail() 方法:
Future<FirebaseUser> signUpWithEmail() async {
String email = emailControlller.text;
String password = passwordControlller.text;
FirebaseUser user = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: emailControlller.text,
password: passwordControlller.text,
)
.then((user) {
// set balance to 0
Firestore.instance
.collection("users")
.document(user.uid)
.setData({"cash": 0});
}).catchError((e) => print("error : $e"));
return user;
}
最佳答案
无论如何,您将返回到 signUpWithEmail()
,您不会抛出错误,因此它永远不会进入
.catchError((error) {
print("THE ERROR : $error");
})
要修复它,您必须在 signUpWithEmail()
上抛出错误。尝试类似的东西。
Future<FirebaseUser> signUpWithEmail() async {
String email = emailControlller.text;
String password = passwordControlller.text;
FirebaseUser user = await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: emailControlller.text,
password: passwordControlller.text,
)
.then((user) {
// set balance to 0
Firestore.instance
.collection("users")
.document(user.uid)
.setData({"cash": 0});
}).catchError((e) => {
print("error : $e")
throw("Your error") // It return to catch
});
return user;
}
如果你能做到,请告诉我。
关于firebase - 如何在 Flutter 中检测异步操作是否成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57657393/