无法捕获 Firebase 电子邮件注册过程中抛出的 Flutter 异常
Unable to catch exception in Flutter thrown during the Firebase Email Sign Up process
我试图在 Flutter 的电子邮件注册过程中捕获 Firebase 抛出的异常。但是异常没有被捕获。我不确定我到底做错了什么。
Future<dynamic> signUpWithEmail({
required BuildContext context,
required String userName,
required String userEmail,
required String userPassword,
}) async {
User? firebaseUser;
try {
authInstance
.createUserWithEmailAndPassword(
email: userEmail,
password: userPassword,
)
.then((value) async {
if (value.user != null) {
await value.user!.updateDisplayName(userName);
await value.user!.reload();
firestoreInstance.collection("users").doc(value.user!.uid).set({
"name": userName,
"photo": null,
"emailAddress": value.user!.email,
"signUpMethod": "email",
"accountCreatedOn": Timestamp.now(),
"receiveOffers": true,
"isAdmin": false,
"isAuth": false,
"isSubscribed": false,
}).then((value) async {
await authInstance
.signInWithEmailAndPassword(
email: userEmail,
password: userPassword,
)
.then((value) {
firebaseUser = authInstance.currentUser;
Navigator.of(context).push(ScaledAnimationPageRoute(HomePage()));
});
});
}
});
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('An account already exists for that email address you are trying to sign up.');
}
} on FirebaseException catch (e) {
print("Firebase Exception thrown on Sign Up page");
print(e.message);
} on PlatformException catch (e) {
print("Platform Exception thrown on Sign Up page");
print(e.message);
} catch (e) {
print("Exception thrown on Sign Up page");
print(e);
}
return firebaseUser;
}
我在控制台中看到以下错误。
E/flutter (19359): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: [firebase_auth/email-already-in-use] The email address is already in use by another account.
E/flutter (19359): #0 MethodChannelFirebaseAuth.createUserWithEmailAndPassword (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:270:7)
E/flutter (19359): <asynchronous suspension>
E/flutter (19359): #1 FirebaseAuth.createUserWithEmailAndPassword (package:firebase_auth/src/firebase_auth.dart:211:7)
E/flutter (19359): <asynchronous suspension>
E/flutter (19359):
E/libEGL (19359): call to OpenGL ES API with no current context (logged once per thread)
谁能帮我解决这个问题。提前感谢您的帮助。
我建议使用 .contains 方法检查 catch 段中来自服务器的响应中的错误类型,例如
if(e.code.contains('WEAK_PASSWORD')){
print('This password is too weak')
}
您需要更新代码以使用 async-await
而不是 .then()
,因为您使用的是 try-catch
.
当您在 Future 上使用 .then()
时,它的错误会被 .catchError()
回调捕获。由于您没有 .catchError()
,错误被记录为未捕获。
您更新后的代码将是:
Future<dynamic> signUpWithEmail({
required BuildContext context,
required String userName,
required String userEmail,
required String userPassword,
}) async {
User? firebaseUser;
try {
final UserCredential value =
await authInstance.createUserWithEmailAndPassword(
email: userEmail,
password: userPassword,
);
if (value.user != null) {
await value.user!.updateDisplayName(userName);
await value.user!.reload();
await firestoreInstance.collection("users").doc(value.user!.uid).set({
"name": userName,
"photo": null,
"emailAddress": value.user!.email,
"signUpMethod": "email",
"accountCreatedOn": Timestamp.now(),
"receiveOffers": true,
"isAdmin": false,
"isAuth": false,
"isSubscribed": false,
});
await authInstance.signInWithEmailAndPassword(
email: userEmail,
password: userPassword,
);
firebaseUser = authInstance.currentUser;
Navigator.of(context).push(ScaledAnimationPageRoute(HomePage()));
}
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print(
'An account already exists for that email address you are trying to sign up.');
}
} on FirebaseException catch (e) {
print("Firebase Exception thrown on Sign Up page");
print(e.message);
} on PlatformException catch (e) {
print("Platform Exception thrown on Sign Up page");
print(e.message);
} catch (e) {
print("Exception thrown on Sign Up page");
print(e);
}
return firebaseUser;
}
我试图在 Flutter 的电子邮件注册过程中捕获 Firebase 抛出的异常。但是异常没有被捕获。我不确定我到底做错了什么。
Future<dynamic> signUpWithEmail({
required BuildContext context,
required String userName,
required String userEmail,
required String userPassword,
}) async {
User? firebaseUser;
try {
authInstance
.createUserWithEmailAndPassword(
email: userEmail,
password: userPassword,
)
.then((value) async {
if (value.user != null) {
await value.user!.updateDisplayName(userName);
await value.user!.reload();
firestoreInstance.collection("users").doc(value.user!.uid).set({
"name": userName,
"photo": null,
"emailAddress": value.user!.email,
"signUpMethod": "email",
"accountCreatedOn": Timestamp.now(),
"receiveOffers": true,
"isAdmin": false,
"isAuth": false,
"isSubscribed": false,
}).then((value) async {
await authInstance
.signInWithEmailAndPassword(
email: userEmail,
password: userPassword,
)
.then((value) {
firebaseUser = authInstance.currentUser;
Navigator.of(context).push(ScaledAnimationPageRoute(HomePage()));
});
});
}
});
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print('An account already exists for that email address you are trying to sign up.');
}
} on FirebaseException catch (e) {
print("Firebase Exception thrown on Sign Up page");
print(e.message);
} on PlatformException catch (e) {
print("Platform Exception thrown on Sign Up page");
print(e.message);
} catch (e) {
print("Exception thrown on Sign Up page");
print(e);
}
return firebaseUser;
}
我在控制台中看到以下错误。
E/flutter (19359): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: [firebase_auth/email-already-in-use] The email address is already in use by another account.
E/flutter (19359): #0 MethodChannelFirebaseAuth.createUserWithEmailAndPassword (package:firebase_auth_platform_interface/src/method_channel/method_channel_firebase_auth.dart:270:7)
E/flutter (19359): <asynchronous suspension>
E/flutter (19359): #1 FirebaseAuth.createUserWithEmailAndPassword (package:firebase_auth/src/firebase_auth.dart:211:7)
E/flutter (19359): <asynchronous suspension>
E/flutter (19359):
E/libEGL (19359): call to OpenGL ES API with no current context (logged once per thread)
谁能帮我解决这个问题。提前感谢您的帮助。
我建议使用 .contains 方法检查 catch 段中来自服务器的响应中的错误类型,例如
if(e.code.contains('WEAK_PASSWORD')){
print('This password is too weak')
}
您需要更新代码以使用 async-await
而不是 .then()
,因为您使用的是 try-catch
.
当您在 Future 上使用 .then()
时,它的错误会被 .catchError()
回调捕获。由于您没有 .catchError()
,错误被记录为未捕获。
您更新后的代码将是:
Future<dynamic> signUpWithEmail({
required BuildContext context,
required String userName,
required String userEmail,
required String userPassword,
}) async {
User? firebaseUser;
try {
final UserCredential value =
await authInstance.createUserWithEmailAndPassword(
email: userEmail,
password: userPassword,
);
if (value.user != null) {
await value.user!.updateDisplayName(userName);
await value.user!.reload();
await firestoreInstance.collection("users").doc(value.user!.uid).set({
"name": userName,
"photo": null,
"emailAddress": value.user!.email,
"signUpMethod": "email",
"accountCreatedOn": Timestamp.now(),
"receiveOffers": true,
"isAdmin": false,
"isAuth": false,
"isSubscribed": false,
});
await authInstance.signInWithEmailAndPassword(
email: userEmail,
password: userPassword,
);
firebaseUser = authInstance.currentUser;
Navigator.of(context).push(ScaledAnimationPageRoute(HomePage()));
}
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('The password provided is too weak.');
} else if (e.code == 'email-already-in-use') {
print(
'An account already exists for that email address you are trying to sign up.');
}
} on FirebaseException catch (e) {
print("Firebase Exception thrown on Sign Up page");
print(e.message);
} on PlatformException catch (e) {
print("Platform Exception thrown on Sign Up page");
print(e.message);
} catch (e) {
print("Exception thrown on Sign Up page");
print(e);
}
return firebaseUser;
}