[英]Closing dialog automatically, after firestore response in flutter
I have created a dialog box in which I'm animating a CircularProgressIndicator, it loads when firebase authentication is being carried out.我创建了一个对话框,其中我正在为 CircularProgressIndicator 设置动画,它在执行 firebase 身份验证时加载。 It all goes fine unless login fails for some reason like (no internet, wrong email/password), app show error dialog then this dialog box keeps displaying unless I tap outside the box.一切都很好,除非由于某种原因登录失败,例如(没有互联网,错误的电子邮件/密码),应用程序显示错误对话框然后这个对话框一直显示,除非我在框外点击。 I want this to automatically close if another dialog box or error box appears.如果出现另一个对话框或错误框,我希望它自动关闭。
Future signIn()async{
try {
showDialog(context: context, builder: (context) =>
AlertDialog(
title: Center(
child: CircularProgressIndicator(
color: Colors.deepOrange,
),
),
)
);
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: emailController.text,
password: passwordController.text,
);
fetchData();
// print(role_firestore);
if ( role_firestore == 'Admin'){
return Navigator.push(
context,
MaterialPageRoute(builder: (context) => AdminHomePage()));
}else if (role_firestore == 'User'){
return Navigator.push(
context,
MaterialPageRoute(builder: (context) => MainPage()));
}else if (role_firestore == 'Restaurant'){
return Navigator.push(context, MaterialPageRoute(builder: (context) =>
Restaurants()));
}else {
Navigator.pop(context);
}
} on FirebaseAuthException catch (e) {
final result = await Connectivity().checkConnectivity();
showConnectivitySnackBar(result);
handleLoginError (e);
}
}
and this is the dialog box which shows firestore errors这是显示firestore错误的对话框
showDialog(context: context, builder: (context)=> AlertDialog(
title: const Text("Log In Failed",style: TextStyle(color: Colors.deepOrange)),
content: Text(messageToDisplay,style: TextStyle(color: Colors.deepOrange)),
actions: [TextButton( onPressed: () {Navigator.of(context, rootNavigator:
true).pop();},
child: const Text("Ok",style: TextStyle(color: Colors.deepOrange)),)],
));
you can close/pop your dialog by calling:您可以通过调用关闭/弹出对话框:
Navigator.of(context).pop();
just put it at the end of the method where the data is loaded when the try succeed and in the start of the catch if an error got caught只需将其放在尝试成功时加载数据的方法的末尾,如果捕获到错误,则将其放在 catch 的开头
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.