flutter - NoSuchMethodError:类“FlutterError”没有实例获取器“代码”。接收方:“FlutterError”实例尝试调用:代码)
问题描述
我一直在尝试来自 GitHub 的示例 Flutter 应用程序代码,以便在 Firebase 上简单地登录和注册用户。每次我在干净构建应用程序后登录或注册时,它都会将我带到主页但抛出此异常发生异常。NoSuchMethodError (NoSuchMethodError: Class 'FlutterError' has no instance getter 'code'. Receiver: Instance of 'FlutterError' 尝试调用: code) 我不知道 'FlutterError' 指的是什么,因为我没有看到任何这样的类。并且在名为“login-register.dart”的文件中出现了两次代码。我附上下面的代码:( 注意:在我热重载应用程序并且用户已经登录后它运行正常,只在第一次抛出异常)
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;
});
}
}
解决方案
您捕获的异常没有code
属性。这仅存在于 firebase 异常实现中,而不存在于一般异常类中。
如果您期望某种类型的错误,您应该明确地捕获该错误并正确处理它,并为所有其他错误设置一个单独的 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) {
...
}
您在共享的代码中调用的方法将抛出FirebaseAuthException
s,如上面的代码所示。
推荐阅读
- reactjs - React 路由器 + Axios 拦截器。如何进行重定向?
- svg - SVG 路径背景
- .net - 预期“MappingStart”,得到“SequenceStart”
- sql - 快速包含另一种类型的先前值
- javascript - javascript(测试对象的属性)
- laravel - 猫头鹰旋转木马吐出单个物品而不是旋转木马
- python - 我正在尝试通过使用当前列表来制作一个大列表,但它不起作用,有人可以解释为什么会发生这种情况吗?
- javascript - 如何在 Sharepoint 中获取文件的文件类型
- javascript - 超过 50 个数据点的 HighChart 中断
- express - axios 没有捕捉到错误