user2671169
user2671169

Reputation: 219

Flutter - ShowDialog not showing the dialog

Here is my _showDialog function:

Future<void> _showDialog({BuildContext context, String msg, String title}) async {
  showDialog<bool>(
      context: context,
      builder: (context) {
        return AlertDialog(
          title: Text(title),
          content: Text(msg),
          actions: [
            FlatButton(
              child: Text('Ok'),
              onPressed: () => Navigator.of(context).pop(true),
            ),
          ],
        );
      });
}

Here is my code:

        if (result.contains("Error")) {
          // Error!!! 
          // Show ERROR dialog box.

          _showDialog(context: context, title: "Error", msg: result);

        } else {
          // Success!!! 
          // Show SUCCESS dialog box, then return to the previous screen.
          
          _showDialog(context: context, title: "Success", msg: "Success!");

          Navigator.pop(context); // return to the previous screen

        }

If error occurs the ERROR dialog box it shows. But if there is no error the SUCCESS dialog box it not show.

If the line

Navigator.pop(context);

is commented out the SUCCESS dialog box it show.

I need to show the SUCCESS dialog box before return to the previous screen.

Upvotes: 0

Views: 1583

Answers (3)

Harsh
Harsh

Reputation: 1

you should await the showDialog like this

Future<void> authSuccessHandle(
      {required String title,
      required String subtitle,
      required BuildContext context}) async {
    await showDialog(
        context: context,
        builder: (BuildContext ctx) {
          return AlertDialog(
            backgroundColor: Colors.green.shade50,
            title: Row(
              children: [
                Padding(
                  padding: const EdgeInsets.only(right: 6.0),
                  child: CircleAvatar(
                    backgroundColor: ColorsConsts.AppMainColor,
                    child: const Icon(
                      Icons.done,
                      color: Colors.white,
                      size: 25,
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Text(
                    title,
                    style: const TextStyle(
                      fontSize: 16,
                    ),
                  ),
                ),
              ],
            ),
            content: Text(
              subtitle,
              style: const TextStyle(
                fontSize: 14,
              ),
            ),
            actions: [
              TextButton(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text(
                  'Ok',
                  style: TextStyle(
                    color: ColorsConsts.AppMainColor,
                    fontSize: 16,
                  ),
                ),
              )
            ],
          );
        });
  }

Upvotes: 0

GraSim
GraSim

Reputation: 4230

I was also having the same issue but adding await in front of showDialog fixed it for me

Upvotes: 0

Cristian Bregant
Cristian Bregant

Reputation: 1906

You should then await the close button response on your dialog, like:

Future _showDialog({BuildContext context, String msg, String title}) async {
return await showDialog<bool>(
  context: context,
  builder: (context) {
    return AlertDialog(
      title: Text(title),
      content: Text(msg),
      actions: [
        FlatButton(
          child: Text('Ok'),
          onPressed: () => Navigator.of(context).pop(true),
        ),
      ],
    );
    });
}

and then

if (result.contains("Error")) {
      // Error!!! 
      // Show ERROR dialog box.

      _showDialog(context: context, title: "Error", msg: result);

    } else {
      // Success!!! 
      // Show SUCCESS dialog box, then return to the previous screen.
      
      await _showDialog(context: context, title: "Success", msg: "Success!");

      Navigator.pop(context); // return to the previous screen

    }

Upvotes: 1

Related Questions