我目前正在測驗我的應用程式,當我嘗試使用已在使用的電子郵件注冊新帳戶時,我收到以下錯誤:
ArgumentError(無效引數(onError):Future.catchError 的錯誤處理程式必須回傳未來型別的值)
當我單擊登錄應用程式時,仍會通知用戶此電子郵件已在使用中,但該錯誤會使應用程式崩潰。如果我嘗試測驗任何其他錯誤,我的應用程式不會崩潰。
由于不確定為什么會發生錯誤,我不太確定如何開始解決此問題。
*。鏢
void signUp(String email, String password) async {
if (GlobalKey<FormState>().currentState!.validate()) {
try {
await FirebaseAuth.instance //<--- Error stops here
.createUserWithEmailAndPassword(email: email, password: password)
.then((value) => {postDetailsToFirestore()})
.catchError((e) {
Fluttertoast.showToast(msg: e!.message);
});
} on FirebaseAuthException catch (error) {
switch (error.code) {
case "invalid-email":
errorMessage = "Your email address appears to be incorrect.";
break;
case "wrong-password":
errorMessage = "Your password is wrong.";
break;
case "user-not-found":
errorMessage = "User with this email doesn't exist.";
break;
case "user-disabled":
errorMessage = "User with this email has been disabled.";
break;
case "too-many-requests":
errorMessage = "Too many requests";
break;
case "operation-not-allowed":
errorMessage = "Signing in with Email and Password is not enabled.";
break;
default:
errorMessage = "An undefined Error happened.";
}
Fluttertoast.showToast(msg: errorMessage!);
print(error.code);
}
}
}
postDetailsToFirestore() async {
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
User? user = _auth.currentUser;
UserModel userModel = UserModel();
// writing all the values
userModel.email = user!.email;
userModel.uid = user.uid;
userModel.userName = NameEditingController.text;
userModel.password = passwordEditingController.text;
await firebaseFirestore
.collection("users")
.doc(user.uid)
.set(userModel.toMap());
Fluttertoast.showToast(msg: "Account created successfully ");
Navigator.pushAndRemoveUntil(
(context),
MaterialPageRoute(builder: (context) => HomeScreen()),
(route) => false);
}
uj5u.com熱心網友回復:
async/await只是處理承諾的語法糖。它使您的代碼更具可讀性,并將其與try/catch您結合使用可以更清晰地處理例外。
如果移動設備無法上網,我也建議處理這種情況,并且SocketException拋出了一個。對于這種例外,您需要import 'dart:io';
不要忘記添加一個 simple catchas,因為你可以有除FirebaseAuthExceptionand以外的例外SocketException,如果你只使用這些將不會被捕獲on。(你不需要catchafter on。)
試試這種方式:
try {
await FirebaseAuth.instance.createUserWithEmailAndPassword(email: email,
password: password);
await postDetailsToFirestore();
} on FirebaseAuthException (error) {
...
} on SocketException {
...
} catch (e) {
..
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433473.html
