creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126674

Why is onWillPop in WillPopScope ignored when using Navigator.pop?

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();

Upvotes: 13

Views: 3376

Answers (1)

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126674

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();

Upvotes: 26

Related Questions