flutter PlatformException错误上的Firebase不工作

66bbxpm5  于 2022-12-05  发布在  Flutter
关注(0)|答案(2)|浏览(134)

登录或注册FirebaseAuth.instance时出现try .. catch阻止
这是我的密码。

try {
   //... code here
} on PlatformException catch (err) {
  var message = 'An error occured, please check your credentials.';

  if (err.message != null) {
    message = err.message!;
  }

  ScaffoldMessenger.of(ctx).showSnackBar(
    SnackBar(
      content: Text(message),
      backgroundColor: Theme.of(ctx).errorColor,
    ),
  );
  // other codes here

} catch (err) {
  print(err);
}

现在的问题是,如果我遇到类似The email address is badly formatted.he email address is already in use by another account的错误,它不会进入on PlatformException catch (err)块,而是进入catch块。
如何确保上面提到的错误类型应该在on PlatformException块中执行?

kzipqqlq

kzipqqlq1#

尝试与此希望你会得到解决方案!

try {
       //... code here
    } on FirebaseAuthException catch (e) {
      String errorMessage = AuthExceptionHandler.handleException(e);
      errorSnackBar(content: errorMessage);
    } catch (e) {
        print(e.toString());
    }

用于根据错误代码管理错误消息AuthExceptionHandler类

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

  ///
  /// Accepts AuthExceptionHandler.errorType and set message according error
  static generateExceptionMessage(exceptionCode) {
    String errorMessage;
    switch (exceptionCode) {
      case AuthResultStatus.invalidEmail:
        errorMessage = StringConstants.emailAddressMalformed.tr;
        break;
      case AuthResultStatus.wrongPassword:
        errorMessage = StringConstants.wrongPassword.tr;
        break;
      case AuthResultStatus.userNotFound:
        errorMessage = StringConstants.userNotExist.tr;
        break;
      case AuthResultStatus.userDisabled:
        errorMessage = StringConstants.userDisable.tr;
        break;
      case AuthResultStatus.tooManyRequests:
        errorMessage = StringConstants.manyRequestTryAfterSomeTime.tr;
        break;
      case AuthResultStatus.operationNotAllowed:
        errorMessage = StringConstants.signInNotEnable.tr;
        break;
      case AuthResultStatus.emailAlreadyExists:
        errorMessage = StringConstants.emailAlreadyExist.tr;
        break;
      default:
        errorMessage = StringConstants.undefinedErrorHappened.tr;
    }

    return errorMessage;
  }
}
u1ehiz5o

u1ehiz5o2#

好的!我已经通过使用on FirebaseAuthException而不是on PlatformException解决了它。希望这能帮助将来遇到同样问题的其他人。

相关问题