0

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.

3 Answers 3

1

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

    }
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.