0

I already read countless links like this one but it does not help.

The use case is very simple, I want to run a function AFTER the alert box has been dismissed.

void dummyFunc()  {

            sleep(Duration(seconds:3));
            print("done");

}

Future<bool> displayDialog() async {
            return showDialog<bool>(

                context: context,
                barrierDismissible: false, // user must tap button!
                builder: (BuildContext context) {
                    return AlertDialog(

                        title: Text('AlertDialog Title'),
                        content: SingleChildScrollView(
                            child: ListBody(
                                children: <Widget>[
                                    Text('This is a demo alert dialog.'),
                                    Text('Would you like to approve of this message?'),
                                ],
                            ),
                        ),
                        actions: <Widget>[
                            FlatButton(
                                child: Text('Decline'),
                                onPressed: () {
                                    Navigator.of(context).pop(false);
                                },
                            ),
                            FlatButton(
                                child: Text('Approve'),
                                onPressed: () {
                                    Navigator.of(context).pop(true);
                                },
                            ),
                        ],
                        elevation: 24.0,
                        shape:RoundedRectangleBorder(),
                    );
                },
            );
        }

var button = AnimatedOpacity(
            opacity: 1.0,
            duration: Duration(milliseconds: 500),
            child: FloatingActionButton(
                tooltip: 'Start',
                child: Icon(iconShape),
                onPressed: () async {
                    bool shouldUpdate = await displayDialog();
                    print(shouldUpdate);
                    if (shouldUpdate)
                          dummyFunc();

                })
        );

But the alert dialog is dismissed 3sd after.

Kindly let me know what I am doing wrong, Thank you~

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81

2 Answers2

1

I think this is happening because you are using sleep. Instead of that use Future delay.

void dummyFunc() {
    print("done");
  }

If you don't want delay, then you can also remove this future, this function will executed after dialog box dismissed.

Sleep will hold the process, that’s why you are facing this error.

Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61
  • the dummy function is just a replacement for a more complex function, but I indeed use some sleep events between animations. Following your message I tried with both Future.delayed and Timer but it is still not working as intended. – Antonin GAVREL May 12 '20 at 04:25
  • just removing future and it will for as you expected. if your process is not async then why do you want to sleep? you print a single statement or write any number of line long code doesn’t matter. it will execute same if it is not async as in this case. @AntoninGAVREL – Viren V Varasadiya May 12 '20 at 04:33
  • the thing is I used a sleep but your answer helped me to figure out – Antonin GAVREL May 12 '20 at 04:35
  • it was not the exact answer, I upvoted your answer for nudging me in the right direction ;) I will accept someone else's answer if there is a really awesome answer for why sleep was blocking the process at the alertbox level – Antonin GAVREL May 12 '20 at 04:41
  • in fact I use a loop in my code so your answer was best, thank you Viren! – Antonin GAVREL May 12 '20 at 04:52
0

Solved it thanks to Viren who gave me a good intuition, Timer works also nicely if you are not using a loop:

void dummyFunc()  {
            Timer(Duration(seconds: 3), () {

                    print("done");
                });
}

Edit: Actually Viren's answer work better with loops! This can work also. Just avoid sleep. Spent 3h on this, now I hate sleep().

Antonin GAVREL
  • 9,682
  • 8
  • 54
  • 81