处理Firebase错误



我不明白如何处理Firebase调用上的错误,即使我设置了try并捕获了我的应用程序冻结…例如,在下面的代码中,我设置了一些尝试和捕捉这个想法:尝试调用这个Firebase函数,如果它工作正常,否则创建一个窗口对话框,告诉用户他们没有注册。但结果是调用失败,应用程序冻结…

这是代码:

Future<void> submit(
context, FirebaseAuth authF, String email, String password) async {
try {
final userCredential = await authF.signInWithEmailAndPassword(
email: email,
password: password,
);
return;
} catch (e) {
if (Platform.isIOS) {
LogInCupertinoDialogue(context);
} else {
LogInAndroidDialogue(context);
}
return;
}
}

我不知道为什么你的应用程序崩溃与你的代码。

但是,您可能想知道在您的情况下,您可以使用FirebaseAuthException捕获Firebase错误。

下面是一个例子:

try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: "someemail@email.com",
password: "password"
);
} on FirebaseAuthException catch  (e) {
print('Error code: ${e.code}');
print(e.message);
}

你应该看看这个问题这里有更多的解决方案来解决你的问题

这是我处理firebase认证错误的方法:首先,你需要为set创建一个枚举,并在SnackBar或其他…

中获取错误信息。
enum AuthStatus {
loading,
successful,
emailAlreadyExists,
wrongPassword,
invalidEmail,
userNotFound,
userDisabled,
operationNotAllowed,
tooManyRequests,
undefined,
}

我们需要一个方法来处理错误在这里:

static handleFireAuthException(e) {
print(e.code);
var status;
switch (e.code) {
case "invalid-email":
status = AuthStatus.invalidEmail;
break;
case "wrong-password":
status = AuthStatus.wrongPassword;
break;
case "user-not-found":
status = AuthStatus.userNotFound;
break;
case "user-disabled":
status = AuthStatus.userDisabled;
break;
case "too-many-requests":
status = AuthStatus.tooManyRequests;
break;
case "operation-not-allowed":
status = AuthStatus.operationNotAllowed;
break;
case "email-already-in-use":
status = AuthStatus.emailAlreadyExists;
break;
default:
status = AuthStatus.undefined;
}
return status;
}

最后一个方法是在监听AuthStatus的情况下获取错误消息所以是errorMessageGenerator:

static generateExceptionMessage(exceptionCode) {
String errorMessage;
switch (exceptionCode) {
case AuthStatus.invalidEmail:
errorMessage = "Your email address appears to be malformed.";
break;
case AuthStatus.wrongPassword:
errorMessage = "Your password is wrong.";
break;
case AuthStatus.userNotFound:
errorMessage = "User with this email doesn't exist.";
break;
case AuthStatus.userDisabled:
errorMessage = "User with this email has been disabled.";
break;
case AuthStatus.tooManyRequests:
errorMessage = "Too many requests. Try again later";
break;
case AuthStatus.operationNotAllowed:
errorMessage = "Message for operation not allowed";
break;
case AuthStatus.emailAlreadyExists:
errorMessage =
"Already created an account in this Email, if is you, you can try sign in.";
break;
default:
errorMessage = "Signing in with Email and Password is not enabled.";
}
return errorMessage;
}

完整代码:

enum AuthStatus {
loading,
successful,
emailAlreadyExists,
wrongPassword,
invalidEmail,
userNotFound,
userDisabled,
operationNotAllowed,
tooManyRequests,
undefined,
}
class AuthExceptionHandler {
static handleFireAuthException(e) {
print(e.code);
var status;
switch (e.code) {
case "invalid-email":
status = AuthStatus.invalidEmail;
break;
case "wrong-password":
status = AuthStatus.wrongPassword;
break;
case "user-not-found":
status = AuthStatus.userNotFound;
break;
case "user-disabled":
status = AuthStatus.userDisabled;
break;
case "too-many-requests":
status = AuthStatus.tooManyRequests;
break;
case "operation-not-allowed":
status = AuthStatus.operationNotAllowed;
break;
case "email-already-in-use":
status = AuthStatus.emailAlreadyExists;
break;
default:
status = AuthStatus.undefined;
}
return status;
}
static generateExceptionMessage(exceptionCode) {
String errorMessage;
switch (exceptionCode) {
case AuthStatus.invalidEmail:
errorMessage = "Your email address appears to be malformed.";
break;
case AuthStatus.wrongPassword:
errorMessage = "Your password is wrong.";
break;
case AuthStatus.userNotFound:
errorMessage = "User with this email doesn't exist.";
break;
case AuthStatus.userDisabled:
errorMessage = "User with this email has been disabled.";
break;
case AuthStatus.tooManyRequests:
errorMessage = "Too many requests. Try again later";
break;
case AuthStatus.operationNotAllowed:
errorMessage = "Message for operation not allowed";
break;
case AuthStatus.emailAlreadyExists:
errorMessage =
"Already created an account in this Email, if is you, you can try sign in.";
break;
default:
errorMessage = "Signing in with Email and Password is not enabled.";
}
return errorMessage;
}
}

相关内容

最新更新