微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter Modb Mobx-Observable在onpress内不会更新

如何解决Flutter Modb Mobx-Observable在onpress内不会更新

我是Flutter的新手,但遇到了问题。
我正在使用mobx。在我的视图中,我有一个按钮,并且在此按钮的内部,
我正在等待showDialog属性更改以显示对话框视图。但是,在onpress内,showdialog不起作用。还有其他方法吗?

我的控制器

@observable
  bool showDialog = false;

@action
  Future callLoginService() async {

      await Future.delayed(Duration(seconds: 6));
    
      showDialog = true;
  }

视图

Observer(
        builder: (_) {
          return Center(
            child: RaisedButton(
              child: Text("TESTE"),onpressed: () async {
                controller.callLoginService();

                if (controller.showDialog) {
                  final action = await InfoDialogView.showAlertDialog(
                      context,"Try again",'Invalid user');

                  if (action == DialogAction.abort) {
                    controller.showDialog = false;
                  }
                }
              },),);
        },

解决方法

这是因为您的 onPressed 方法是异步的,但您没有在 controller.callLoginService() 之前使用 'await' 关键字。

Observer(
    builder: (_) {
      return Center(
        child: RaisedButton(
          child: Text("TESTE"),onPressed: () async {
            await controller.callLoginService(); //put await for calling asynchronous methods

            if (controller.showDialog) {
              final action = await InfoDialogView.showAlertDialog(
                  context,"Try again",'Invalid user');

              if (action == DialogAction.abort) {
                controller.showDialog = false;
              }
            }
          },),);
    },

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。