I use a WillPopScope
to try to stop a route from popping:
WillPopScope(
onWillPop: () async {
print('This is never called');
return false;
}
)
When I use Navigator.pop
, the current route is simply popped instead of onWillPop
being called:
Navigator.of(context).pop();
Pop the top-most route off the navigator that most tightly encloses the given context. The current route's Route. didPop method is called first. If that method returns false, then the route remains in the Navigator's history (the route is expected to have popped some internal state; see e.g. LocalHistoryRoute).
In the onWillPop callback, we'll do two things: Display an alert dialog to the user with a question and ask them to click an action button (either Yes or No) Use the response from the dialog to pop the ContentView.
In Flutter these elements are called routes and they're managed by a Navigator widget. The navigator manages a stack of Route objects and provides two ways for managing the stack, the declarative API Navigator. pages or imperative API Navigator. push and Navigator.
This is by design.
You will have to use Navigator.maybePop
:
Tries to pop the current route of the navigator that most tightly encloses the given context, while honoring the route's Route.willPop state.
This means that only Navigator.maybePop
honors onWillPop
and Navigator.pop
does not:
Navigator.of(context).maybePop();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With