简体   繁体   English

在 flutter 中的 Firestore 响应后自动关闭对话框

[英]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.

相关问题 在 Flutter 中更新 firestore 后,项目无法编译 - After firestore update in Flutter the project doesnt compile 24 小时后自动更改 Firestore 中的变量 - Swift - Automatically change a variable in Firestore after 24 hours - Swift 24 小时后自动更改 Firestore 中的变量 - Android 中的 Java - Automatically change a variable in Firestore after 24 hours - Java in Android 为什么在 Firestore Flutter 中更新数据后 DateTime 会转换为 TimeStamp? - Why DateTime is converted to TimeStamp after updating data in Firestore Flutter? 如果使用 flutter 在云 firestore 中更新数据,是否有一种方法可以自动更新页面中的数据 - Is there a way to automatically update the data in the page if a data is updated in cloud firestore using flutter 从 flutter 应用程序在 firestore 中更新数据后无法读取数据 - Cannot read data after updating it in firestore from flutter app 我想在 flutter 中从 firestore 获取此数据后对数据进行洗牌 - I want to shuffle the data after getting this data from firestore in flutter Flutter:注册成功后如何让用户自动登录? - Flutter: How to make user login automatically after register successfully? Flutter web Firebase Firestore - Flutter web Firebase Firestore Flutter 多个 Firestore 查询 - Flutter Multiple Firestore Queries
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM