处理 Firebase 错误 Flutter

时间:2021-02-15 14:41:52

标签: firebase flutter firebase-authentication

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

这是代码:

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;
    }
  }

2 个答案:

答案 0 :(得分:0)

我不确定为什么您的应用会因您的代码而崩溃。

但是,您可能想知道在您的情况下可以使用 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);
}

答案 1 :(得分:0)

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

这是我处理 firebase 身份验证错误的方法: 首先,您需要为设置创建一个枚举并在 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;
  }
}