How to dismiss an AlertDialog on a FlatButton click?

Gustash picture Gustash · May 24, 2017 · Viewed 51.5k times · Source

I have the following AlertDialog.

showDialog(
            context: context,
            child: new AlertDialog(
              title: const Text("Location disabled"),
              content: const Text(
                  """
Location is disabled on this device. Please enable it and try again.
                  """),
              actions: [
                new FlatButton(
                  child: const Text("Ok"),
                  onPressed: _dismissDialog,
                ),
              ],
            ),
        );

How can I make _dismissDialog() dismiss said AlertDialog?

Answer

Collin Jackson picture Collin Jackson · May 24, 2017

Navigator.pop() should do the trick. You can also use that to return the result of the dialog (if it presented the user with choices)