首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Flutter Modular Mobx - Observable不会在onpress内部更新

Flutter Modular Mobx - Observable不会在onpress内部更新
EN

Stack Overflow用户
提问于 2020-08-23 11:48:57
回答 1查看 207关注 0票数 0

我是Flutter的新手,我遇到了一个问题。

我用的是mobx。在我看来,我有一个按钮,在这个按钮里面,

我正在等待showDialog属性更改,以便显示对话框视图。但是,在onpress中,显示对话框不起作用。有没有其他方法可以做到这一点?

我的控制器

代码语言:javascript
复制
@observable
  bool showDialog = false;

@action
  Future callLoginService() async {

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

视图

代码语言:javascript
复制
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;
                  }
                }
              },
            ),
          );
        },
      ),
EN

回答 1

Stack Overflow用户

发布于 2021-05-12 18:06:43

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

代码语言:javascript
复制
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;
              }
            }
          },
        ),
      );
    },
  ),
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63543179

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档