小编典典

如何处理Flubase上的Firebase Auth异常

flutter

请问有人知道如何在flutter上捕获firebase Auth异常并显示它们吗?

注意:我对控制台不感兴趣(catcherror((e)print(e))

我需要更有效的东西,例如“用户不存在”,以便可以将其传递给字符串并显示出来。

已经处理了几个月。

提前致谢。

我试图用// errorMessage = e.toString();替换print(e); 然后将其传递给功能,所有的努力都是徒劳的。

    FirebaseAuth.instance
              .signInWithEmailAndPassword(email: emailController.text, password: passwordController.text)
              .then((FirebaseUser user) {
                _isInAsyncCall=false;
            Navigator.of(context).pushReplacementNamed("/TheNextPage");

          }).catchError((e) {
           // errorMessage=e.toString();
            print(e);
            _showDialog(errorMessage);

            //exceptionNotice();
            //print(e);

我希望能够提取异常消息并将异常消息传递到一个对话框,然后可以向用户显示该对话框。


阅读 263

收藏
2020-08-13

共1个答案

小编典典

(21/02/20)编辑:这个答案很旧,其他答案包含跨平台解决方案,因此您应该首先看看他们的解决方案,并将其视为后备解决方案。

firebase auth插件实际上还没有合适的跨平台错误代码系统,因此您必须独立处理android和ios的错误。

我当前正在使用此github问题的临时修复程序:#20223

请注意,因为它是 临时 修复程序,所以不要指望它作为永久解决方案是完全可靠的。

enum authProblems { UserNotFound, PasswordNotValid, NetworkError }

try {
  FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(
      email: email,
      password: password,
  );
} catch (e) {
  authProblems errorType;
  if (Platform.isAndroid) {
    switch (e.message) {
      case 'There is no user record corresponding to this identifier. The user may have been deleted.':
        errorType = authProblems.UserNotFound;
        break;
      case 'The password is invalid or the user does not have a password.':
        errorType = authProblems.PasswordNotValid;
        break;
      case 'A network error (such as timeout, interrupted connection or unreachable host) has occurred.':
        errorType = authProblems.NetworkError;
        break;
      // ...
      default:
        print('Case ${e.message} is not yet implemented');
    }
  } else if (Platform.isIOS) {
    switch (e.code) {
      case 'Error 17011':
        errorType = authProblems.UserNotFound;
        break;
      case 'Error 17009':
        errorType = authProblems.PasswordNotValid;
        break;
      case 'Error 17020':
        errorType = authProblems.NetworkError;
        break;
      // ...
      default:
        print('Case ${e.message} is not yet implemented');
    }
  }
  print('The error is $errorType');
}
2020-08-13