我正在尝试处理 firebase 身份验证错误,例如密码错误等 但是当我输入错误的密码时,应用程序会冻结并出现异常,但我想在小吃店中显示它 这是我的代码
Future <bool> sign InWithEmailAndPassword(String userEmail , String userPassword ) async {
try {
await _firebaseAuth.sign InWithEmailAndPassword (email : userEmail , password : userPassword );
return true;
} on FirebaseAuthException catch (e) {
print (e);
return false;
}
}
这里
bool result = await auth.signInWithEmailAndPassword ( _emailController.text.trim() , _passwordController.text.trim ();
if ( result == true ) {
Navigator.of(context).push(MaterialPageRoute(builder:(context) => HomeView ());
} else {
ScaffoldMessenger.of(context)showSnackBar(SnackBar(content: Text(' Something went wrong ! ' ));
}
}
并且我尝试分配我在变量中捕获的错误的值并检查它是否为空以显示错误消息但它没有用!
答案 0 :(得分:0)
你能用这段代码吗,它应该能捕获错误
Future <bool> signInWithEmailAndPassword(String userEmail , String userPassword )async {
try {
await _firebaseAuth.signInWithEmailAndPassword (email : userEmail , password : userPassword );
return true;
} catch (error) {
switch (error.code) {
case "auth/invalid-email":
case "auth/user-disabled":
case "auth/user-not-found":
print(error.message); //for email
break;
case "auth/wrong-password":
print(error.message); // for password
break;
}
}
}
或者这也应该适用于;
Future <bool> signInWithEmailAndPassword(String userEmail , String userPassword )async {
try {
await _firebaseAuth.signInWithEmailAndPassword (email : userEmail , password : userPassword );
return "Success";
} on FirebaseAuthException catch (e) {
return e.message;
} catch (e) {
rethrow;
}
}