AlertDialog not being shown inside aync function(await used)

50 Views Asked by At

So my code is as follows :

Future<bool> showExitPopup(BuildContext context) async {
    debugPrint('showExitPopup');
    debugPrint('context: $context');

    AlertDialog alert = AlertDialog(
      title: Text("Exit App"),
      content: Text("Do you want to exit an App?"),
      actions: [
        ElevatedButton(
          onPressed: () => Navigator.of(context).pop(false),
          //return false when click on "NO"
          child: Text('No'),
        ),
        ElevatedButton(
          onPressed: () => Navigator.of(context).pop(true),
          //return true when click on "Yes"
          child: Text('Yes'),
        ),
      ],
    );

    debugPrint('alert: $alert');

    bool exitApp = await showDialog(
          //show confirm dialogue
          //the return value will be from "Yes" or "No" options
          useSafeArea: true,
          barrierDismissible: false,
          context: context,
          builder: (context) => alert,
        ) ??
        false;

    debugPrint('exitApp: $exitApp');
    return exitApp;
  }

This is function of onWillPop of WillPopScope widget. There are total 4 debugPrint statements. The first 3 are getting printed, but not the last one, and the app never closes. No Alert Dialog is being shown. If anyone can help me this, will be really glad. Thank you.

I expect when the Back button is pressed. Alert Dialog should pop up. Based on user input, the app will either exit or not. But it's not working as expected.

0

There are 0 best solutions below