NoSuchMethodError: Class 'FlutterError' has no instance getter 'code'. Receiver: Instance of 'FlutterError' Tried calling: code)

NoSuchMethodError: Class 'FlutterError' has no instance getter 'code'. Receiver: Instance of 'FlutterError' Tried calling: code)

我一直在尝试使用 GitHub 中的示例 Flutter 应用程序代码来简单地在 Firebase 上登录和注册用户。每次我在干净构建应用程序后登录或注册时,它都会将我带到主页但抛出此异常 Exception has occurred。 NoSuchMethodError(NoSuchMethodError:Class 'FlutterError' 没有实例 getter 'code'。接收方:'FlutterError' 的实例已尝试调用:代码) 我不知道 'FlutterError' 指的是什么,因为我没有看到任何这样的 class。并且在名为 'login-register.dart' 的文件中出现了两次 code。我附上下面的代码: (注意:在我热重载应用程序并且用户已经登录后它运行正常,只在第一次抛出异常)

void _validateLoginInput() async {
      final FormState form = _formKey.currentState;
      if (_formKey.currentState.validate()) {
        form.save();
        _sheetController.setState(() {
          _loading = true;
        });
        try {
          final FirebaseUser user = (await FirebaseAuth.instance
              .signInWithEmailAndPassword(email: _email, password: _password)).user;
          // final uid = user.uid;
          Navigator.of(context).pushReplacementNamed('/home');
        } catch (error) {
          switch (error.code) {
            case "ERROR_USER_NOT_FOUND":
              {
                _sheetController.setState(() {
                  errorMsg =
                      "There is no user with such entries. Please try again.";

                  _loading = false;
                });
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        content: Container(
                          child: Text(errorMsg),
                        ),
                      );
                    });
              }
              break;
            case "ERROR_WRONG_PASSWORD":
              {
                _sheetController.setState(() {
                  errorMsg = "Password doesn\'t match your email.";
                  _loading = false;
                });
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        content: Container(
                          child: Text(errorMsg),
                        ),
                      );
                    });
              }
              break;
            default:
              {
                _sheetController.setState(() {
                  errorMsg = "";
                });
              }
          }
        }
      } else {
        setState(() {
          _autoValidate = true;
        });
      }
    }
void _validateRegisterInput() async {
      final FormState form = _formKey.currentState;
      if (_formKey.currentState.validate()) {
        form.save();
        _sheetController.setState(() {
          _loading = true;
        });
        try {
          final FirebaseUser user = (await FirebaseAuth.instance
              .createUserWithEmailAndPassword(
                  email: _email, password: _password)).user;
          UserUpdateInfo userUpdateInfo = new UserUpdateInfo();
          userUpdateInfo.displayName = _displayName;
          user.updateProfile(userUpdateInfo).then((onValue) {
            Navigator.of(context).pushReplacementNamed('/home');
            Firestore.instance.collection('users').document().setData(
                {'email': _email, 'displayName': _displayName}).then((onValue) {
              _sheetController.setState(() {
                _loading = false;
              });
            });
          });
        } catch (error) {
          switch (error.code) {
            case "ERROR_EMAIL_ALREADY_IN_USE":
              {
                _sheetController.setState(() {
                  errorMsg = "This email is already in use.";
                  _loading = false;
                });
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        content: Container(
                          child: Text(errorMsg),
                        ),
                      );
                    });
              }
              break;
            case "ERROR_WEAK_PASSWORD":
              {
                _sheetController.setState(() {
                  errorMsg = "The password must be 6 characters long or more.";
                  _loading = false;
                });
                showDialog(
                    context: context,
                    builder: (BuildContext context) {
                      return AlertDialog(
                        content: Container(
                          child: Text(errorMsg),
                        ),
                      );
                    });
              }
              break;
            default:
              {
                _sheetController.setState(() {
                  errorMsg = "";
                });
              }
          }
        }
      } else {
        setState(() {
          _autoValidate = true;
        });
      }
    }

您遇到的错误不是 FirebaseError,而是 FlutterError。这意味着,它没有实现 code 字段。 你可以简单地把

if(!(error is FirebaseError)){
  print(error.message); // this is the actual error that you are getting
}

正下方 catch(error) {(在两个文件中)处理此问题。

但是,您似乎遇到了另一个您可能想要处理的 Flutter 错误。它现在应该打印到控制台。

您捕获的异常没有 code 属性。这只存在于 firebase 异常实现中,而不存在于一般异常 class.

如果您预计会出现某种类型的错误,您应该明确地捕获该错误并正确处理它,并为所有其他错误设置一个单独的 catch 块。

这可以通过 on ... catch 块来完成:

try {
  final FirebaseUser user = (await FirebaseAuth.instance
    .signInWithEmailAndPassword(email: _email, password: _password)).user;
  // final uid = user.uid;
  Navigator.of(context).pushReplacementNamed('/home');
} on FirebaseAuthException catch (error) {
  ...
} catch(e) {
  ...
}

您在共享代码中调用的方法将抛出 FirebaseAuthExceptions,如上面的代码所示。