4

I'm using Navigation 2 and setUrlStrategy(PathUrlStrategy()); I added WillPopScope but the onWillPop is not called when clicking the browser's back button.

  Widget build(context) {
return WillPopScope(
  onWillPop: () async {
    print('here');
    if (currentStep == 0) {
      return false;
    }
    return true;
  },
  child: Scaffold(...
Ken White
  • 123,280
  • 14
  • 225
  • 444
Rony Tesler
  • 1,207
  • 15
  • 25
  • 2
    This is a very important question. `WillPopScope` works as expected when clicking the `AppBar` back button but does nothing when the browser's native back button is clicked. From the user's perspective, these are 2 identical interactions yet the app behaves differently. I tried connecting down to the javascript and using `onBeforeUnload` but it's never triggered. Perhaps because of Navigation 2, Auto Route, etc. using the history and not really unloading the page. – Noland Sep 01 '21 at 23:33

1 Answers1

0

In Navigator 2.0, web browsers are chronological and basically treat all navigation as pushes.

Scenario:

  1. Navigate from screen 1 to screen 2
  2. Click the browser's back button.

In this scenario, Flutter will push the previous route (screen 1) onto the stack.

In other words, the browser back button no longer triggers onPopRoute. Instead it triggers onPushRoute.

I believe it was designed this way so that every navigation action can be treated as a deeplink. With that in mind, if you're developing a Flutter app for the web you should avoid passing arguments from one route to another as much as possible.

https://github.com/flutter/flutter/issues/71122#issuecomment-733864359

Code on the Rocks
  • 11,488
  • 3
  • 53
  • 61