我正在为我的应用程序注册屏幕。我实现了Firebase,可以对用户进行身份验证并将某些用户信息成功保存到Firebase数据库中。我开始检查userName字段是否为空。但是,firebase有时无法捕获有关格式错误的电子邮件地址的异常。每次都可使用密码(最少6个字符),但有时也可用于电子邮件。我找不到任何解决方案。这是我的代码。有谁有主意吗?
onPressed: () async {
if (validateName(userName) && validateEmail(email)) {
setState(() {
showSpinner = true;
});
try {
final newUser =
await _auth.createUserWithEmailAndPassword(
email: email, password: password);
if (newUser != null) {
//get name and update user profile
UserUpdateInfo userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = userName;
FirebaseUser user = await _auth.currentUser();
await user.updateProfile(userUpdateInfo);
await user.reload();
Navigator.pushNamed(context, NavigationScreen.id);
}
setState(() {
showSpinner = false;
});
} on PlatformException catch (e) {
setState(() {
showSpinner = false;
});
Fluttertoast.showToast(
msg: e.message,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 3,
backgroundColor: Colors.white,
textColor: Colors.red,
fontSize: 18.0,
);
}
} else {
Fluttertoast.showToast(
msg: 'You must ente all information',
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 3,
backgroundColor: Colors.white,
textColor: Colors.red,
fontSize: 18.0,
);
}
},
//name TextField validation
bool validateName(String name) {
if (name != null && name.length > 2) {
return true;
} else {
return false;
}
}
//email TextField validation
bool validateEmail(String email) {
if (email != null) {
return true;
} else {
return false;
}
}
答案 0 :(得分:1)
resolvers.js
返回一个resolvers.js
,要捕获错误,您可以执行以下操作:
createUserWithEmailAndPassword
使用catchError
,它将处理此Future发出的错误,然后Future<AuthResult>
将显示一个包含错误的对话框。