Flutter 我想在flifter中的Catch块中添加一个警报对话框
此代码用于FireBase注册Flutter 我想在flifter中的Catch块中添加一个警报对话框,flutter,Flutter,此代码用于FireBase注册 onPressed: () async { if (_emailController.text.isEmpty || _passwordController.text.isEmpty) { throw _showDialog2(context); } FirebaseUser user; if (user == null) { try { user = await _fireb
onPressed: () async {
if (_emailController.text.isEmpty || _passwordController.text.isEmpty) {
throw _showDialog2(context);
}
FirebaseUser user;
if (user == null) {
try {
user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
_showDialog(context);
await user.sendEmailVerification();
} on PlatformException catch (e) {
switch (e.code) {
case "ERROR_EMAIL_AlREADY_IN_USE":
setState(() {
errorMsg = "This email is already in use.";
});
_showDialog1(context);
}
print(e.message);
}
}
},
Future<Void> _showDialog1(BuildContext context) {
return showDialog<void>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Alert'),
content: const Text('User Already Exists'),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginPage(),
),
);
},
),
],
);
},
);
}
捕捉颤振是否支持警报对话框,或者我必须为此尝试其他方法?
V/FA(23130):记录用户参与度,ms:67474
V/FA(23130):连接到远程服务
V/FA(23130):活动暂停,时间:479832386
D/FA(23130):日志事件(FE):用户参与(_e),捆绑[{firebase_事件_原点(_o)=自动,参与时间_毫秒(_et)=67474,firebase_屏幕_类(_sc)=主活动,firebase_屏幕_id(_si)=8428444267785459928}]
V/FA(23130):连接尝试已在进行中
D/FA(23130):连接到远程服务
V/FA(23130):处理排队服务任务:2
V/FA(23130):不活动,与服务断开
V/FA(23130):连接到远程服务
V/FA(23130):活动恢复,时间:48057039
switch (e.code) {
case "ERROR_EMAIL_ALREADY_IN_USE":
setState(() {
errorMsg = "This email is already in use.";
});
_showDialog1(context);
}
不能抛出对话框。它是一个小部件,您必须在屏幕上呈现它。所以只要打电话给你的alertdialog
你必须使用未来而不是未来
您可以使用的其他firebase例外情况包括:
- EMAIL_EXISTS
- OPERATION_NOT_ALLOWED
- TOO_MANY_ATTEMPTS_TRY_LATER
- EMAIL_NOT_FOUND
- INVALID_PASSWORD
- USER_DISABLED
首先,将
Future
更改为Future
,因为您需要使用小写的void,否则有时会出现错误,例如“Future”不是“Future”类型的子类型
现在,我尝试了你的代码,它可以工作了,你确定e.code
是ERROR\u EMAIL\u已经在使用中了吗
例如,除单词中的“l”以外的所有字符都是大写的
另外,我建议您使用error constants class
来处理这种情况。即使在删除throw之后,它也不会显示警报。请建议任何替代解决方案请添加您的_showDialog1()方法。此外,在交换机案例中添加打印日志,以检查其是否针对platformException。请检查我是否已添加对话框和日志
onPressed: () async {
if (_emailController.text.isEmpty ||
_passwordController.text.isEmpty) {
_showDialog2(context);
}
FirebaseUser user;
if (user == null) {
try {
user = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
_showDialog(context);
await user.sendEmailVerification();
} catch (e) {
_showDialog1(context);
}
}
},
onPressed: () async {
if (_emailController.text.isEmpty ||
_passwordController.text.isEmpty) {
_showDialog2(context);
}
FirebaseUser user;
if (user == null) {
try {
user = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
_showDialog(context);
await user.sendEmailVerification();
} catch (e) {
_showDialog1(context);
}
}
},