dart - Flutter:无法从 Firebase 身份验证中获取错误代码?

标签 dart flutter

我正在尝试在我的 flutter 应用程序中使用 Google 的 Firebase 身份验证。但是,在处理用户登录时,我似乎找不到一个“代码”来区分我要处理的异常类型。

Loginhandler.dart:

class LoginHandler {
  String signup(_username, _password) async {
    bool _valid = false;
    final prefs = await SharedPreferences.getInstance();
    FirebaseAuth.instance.createUserWithEmailAndPassword(
          email: _username, password: _password)
          .catchError((e) {
            print(e);
            print(e.message);
            print(e.code);
            print(e.details);
          });
...

错误输出:

W/BiChannelGoogleApi(26738): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzal@4e727e7
W/IInputConnectionWrapper(26738): getCursorCapsMode on inactive InputConnection
I/flutter (26738): PlatformException(exception, The email address is already in use by another account., null)
I/flutter (26738): The email address is already in use by another account.
I/flutter (26738): exception
I/flutter (26738): null

I/flutter (26738): PlatformException(exception, The given password is invalid. [ Password should be at least 6 characters ], null)
I/flutter (26738): The given password is invalid. [ Password should be at least 6 characters ]
I/flutter (26738): exception
I/flutter (26738): null

我关注了this突出显示我可以使用 switch 语句的堆栈溢出线程,但上述错误输出没有可供我使用的错误“代码”。

最佳答案

我可以在 Android 上访问错误的“代码”属性。

      try {
          // login logic
      } catch (e) {
        print('Error: $e');
        setState(() {
          _isLoading = false;
          if (_isIos) {
            _errorMessage = e.details;
          } else
            _errorMessage = e.message;
          print(e.code); // can access the code here
        });
      }

关于dart - Flutter:无法从 Firebase 身份验证中获取错误代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51009327/

相关文章:

flutter - 如何在 flutter block 模式中重用以前的状态字段?

android - 三星手机中的 Flutter 构建失败,Permission Denial : broadcast from android asks to run as user -1 but is calling from user 0

flutter - 如何在 Flutter 中进行模块化以分离每个应用程序功能

flutter - FutureBuilder 返回数据后如何导航到屏幕?

Flutter 更新 Text Widget

unit-testing - 如何在 Dart 中测试抛出异常的函数?

flutter - 错误 : The argument type 'String Function(String)' can't be assigned to the parameter type 'String? Function(String?)?'

dart - flutter 文本字段对齐

flutter - 如何创建 Flutter 最小化的应用控件

dart - 如何在 Flutter 中自定义数字选择器?