Is there a way to tie or bind the Navigator.pop()
method to a specific route?
In my case, Navigator.pop()
is called from an async function in a widget which takes some time to complete, such as:
ElevatedButton(
onPressed: () async {
await doSomethingAsync();
if (mounted) {
Navigator.pop(context);
}
},
child: const Text("Do something async then pop")),
)
While that function is ongoing, it could happen however that:
- The route could be popped ahead of time by the user pressing the Android back button (similarly to a previous question I made)
- Another route could be pushed on top of the existing one by the user performing another action
In both cases, I'd like the navigator to "tie" to the specific route onto which the widget is displayed, and only pop it if it isn't yet.
What's the best way to do that? If that's not possible (or overly complicated) I am also open to solutions using the go_router package, for which the same question applies.