【问题标题】:Flutter Modular Mobx - Observable doesn't update inside of onpressFlutter Modular Mobx - Observable 不会在 onpress 内部更新
【发布时间】:2020-08-23 03:48:57
【问题描述】:

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

我的控制器

@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;
                  }
                }
              },
            ),
          );
        },
      ),

【问题讨论】:

    标签: flutter mobx


    【解决方案1】:

    这是因为你的 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;
                  }
                }
              },
            ),
          );
        },
      ),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多