0

I am new with Flutter and I am having a problem.
I'm using mobx. In my view I have a button and inside of this button,
I am waiting for the showDialog property to change in order to show the dialog view . However, within onpress the showdialog does not work. Is there any other way to do this?

My controller

@observable
  bool showDialog = false;

@action
  Future callLoginService() async {

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

view

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;
                  }
                }
              },
            ),
          );
        },
      ),
user6076
  • 15
  • 5

1 Answers1

1

This is because your onPressed method is asynchronous but you haven't used 'await' keyword ahead of controller.callLoginService().

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