在我的应用中,身份验证后,用户可以移至下一个屏幕。
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;
}
答案 0 :(得分:1)
无论如何,您都将返回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;
}
让我知道您能否做到。
答案 1 :(得分:0)
使用FutureBuilder小部件并将逻辑包装在builder方法中