Remove a specific dialog in Flutter

Viewed 607

Inside an app we have various dialogs. In one case for example:

The user clicks a button and gets a "loading dialog". The client gets a news notification as a "notification dialog". The user gets the loaded data and and gets forwarded to a page showing the data.

So the "history"/"hierarchie" should be ["loading dialog", "notification dialog", "data page"]

When the data is loaded we want to remove the "loading dialog" which is in the history below the other dialog and below the data page.

How can I tell a specific dialog in flutter to close?

Currently the page showing the data has a named route and the dialogs are opened via showDialog(...)

1 Answers

You should be able to use Navigator.of(context).pop() to remove dialogs that come from showDialog(...). So if you call it before you push the new page on it will work. You'll need to ensure you have a build context to use though, but otherwise it will work.

This also works for anything that's pushed onto the Navigator stack, like a modal bottom sheet via showModalBottomSheet(...).

Related