android - 对话框没有关闭

标签 android ios flutter

单击 alertdialog 的 Ok 按钮时,我有一个 AlertDialog 我正在从服务器获取数据。在收到数据之前,我正在显示另一个加载对话框。但是一旦收到数据,加载对话框不会关闭。

这是我的代码:

 final GlobalKey<State> _keyLoader = new GlobalKey<State>();


 Future<void> _handleSubmit(BuildContext context,id) async {
showDialog(
    context: context,
    builder: (BuildContext dialogcontext) {
      return AlertDialog(
        title: Text(
          'Topup',
        ),
        content: Text('Are you sure?'),
        actions: <Widget>[
          new FlatButton(
            child: new Text('Ok'),
            onPressed: () async {
              Navigator.of(dialogcontext).pop();
              _submit(dialogcontext, id);
            },
          ),
          new FlatButton(
            child: new Text('Cancel'),
            onPressed: () {
              Navigator.of(dialogcontext).pop();
            },
          )
        ],
      );
    });
  }

 Future<void> _submit(BuildContext context, int id) async {
try {
  Dialogs.showLoadingDialog(
      context, _keyLoader, "Please Wait..."); //invoking login
  await getData(id, context);
  Navigator.of(_keyLoader.currentContext, rootNavigator: true)
      .pop(); //close the dialoge

} catch (error) {
  print(error);
}
}

这是我的 Dialogs 类,用于显示加载对话框

 class Dialogs {
  static Future<void> showLoadingDialog(
  BuildContext context, GlobalKey key, String textToShow) async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return new WillPopScope(
          onWillPop: () async => false,
          child: SimpleDialog(key: key,
              // backgroundColor: Colors.black54,
              children: <Widget>[
                Center(
                  child: Column(children: [
                    CircularProgressIndicator(
                      //backgroundColor: Colors.purple,
                      valueColor: new AlwaysStoppedAnimation<Color>(
                          Colors.pinkAccent[400]),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Text(
                      textToShow,
                      style: TextStyle(color: Colors.pinkAccent[400]),
                    )
                  ]),
                )
              ]));
    });
  }
}

我还在日志中得到:

 Looking up a deactivated widget's ancestor is unsafe.
 I/flutter (21400): At this point the state of the widget's element tree is no longer stable.
 I/flutter (21400): To safely refer to a widget's ancestor in its dispose() method, save a 
 reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's 
 didChangeDependencies() method.

最佳答案

试试这个,阅读 TODO 评论

Future<void> _handleSubmit(BuildContext context, id) async {
  showDialog(
    context: context,
    builder: (BuildContext dialogContext) {
      return AlertDialog(
        title: Text('Topup'),
        content: Text('Are you sure?'),
        actions: <Widget>[
          new FlatButton(
            child: new Text('OK'),
            onPressed: () async {
              Navigator.of(dialogContext).pop();
              _submit(context, id); //TODO: Don't send dialog context since we already did pop on it
            },
          ),
          new FlatButton(
            child: new Text('Cancel'),
            onPressed: () {
              Navigator.of(dialogContext).pop();
            },
          )
        ],
      );
    },
  );
}

Future<void> _submit(BuildContext context, int id) async {
  try {
    Dialogs.showLoadingDialog(
        context, _keyLoader, "Please Wait..."); //invoking login
    await getData(id, context);
    await Future.delayed(Duration.zero); //TODO: Add this line
    Navigator.of(context).pop(); //TODO: Use the received context
  } catch (error) {
    print(error);
  }
}

关于android - 对话框没有关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660045/

相关文章:

android - 如何将 Activity 代码更改为 fragment 代码?

java - 在 RecyclerView 中重置先前 CardView 的颜色

ios - iPhone 解压码

ios - 在 ViewWillAppear 完成运行之前调用 CellForRowAtIndexPath

flutter - Dart/Flutter 中的异步/等待/然后

android - 在 Gradle 中为 x86 android 设备配置 Scrypt

android - 如何将广告转换到我的android应用程序?

ios - NSSortDescriptor 未应用于在 iOS 5.1 下获取请求结果(但在 iOS 6+ 下工作正常)

android - Flutter 的可重用组件

ios - 无法构建 Flutter 应用程序 - 没有这样的模块 'heresdk'