Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

WillPopScope is deprecated: replacing it with PopScope

ErrorWillPopScope is deprecated

Short answer

WillPopScope decided at pop time, PopScope decides ahead of time. Set canPop to true or false, and use onPopInvokedWithResult to react after a pop attempt. For a confirm-before-leaving dialog, use canPop false and call Navigator.pop yourself once the user agrees.

  • Applies to Flutter 3.35, Dart 3.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Let the analyzer list every place that still uses the old API, then fix them one by one with the matching cause below.

    flutter analyze | grep -i -E "willpop|onPopInvoked"
  2. 2Match what you see, then jump to its fix

    1. 1onWillPop just returns a bool that is already knownWillPopScope with a fixed conditionFix
    2. 2onWillPop awaits a dialog and returns its answerWillPopScope used to show a confirm dialogFix
    3. 3onPopInvoked is flagged as deprecatedThe deprecated onPopInvoked callbackFix
    4. 4The warning points at a Form, or at a WillPopScope around an inner NavigatorForm.onWillPop and nested navigatorsFix

What does the error look like?

  • WillPopScope is deprecated
  • onPopInvoked is deprecated
  • onWillPop is deprecated

Cause 1 of 4

WillPopScope with a fixed condition

1Check

If the callback only returns a flag, nothing async is needed.

grep -rn "WillPopScope" lib/

2Fix

Pass the flag as canPop. The check now happens before the pop, not during it.

PopScope(
  canPop: _myCondition,
  child: const MyPage(),
)

3Verify

Run the analyzer again. The warning for that widget is gone.

flutter analyze

Confirmed by docs.flutter.dev

Did this fix it?

Cause 2 of 4

WillPopScope used to show a confirm dialog

1Check

PopScope cannot wait for an answer, because it must know canPop up front. onPopInvokedWithResult runs after the pop attempt was handled.

grep -rn "showDialog" lib/ | head

2Fix

Set canPop to false. In the callback, return when didPop is true, otherwise ask the user and call pop yourself.

PopScope(
  canPop: false,
  onPopInvokedWithResult: (bool didPop, Object? result) async {
    if (didPop) return;
    final NavigatorState navigator = Navigator.of(context);
    final bool? shouldPop = await _showBackDialog();
    if (shouldPop ?? false) {
      navigator.pop();
    }
  },
  child: child,
)

3Verify

Press back: the dialog shows. Confirm, and the page closes.

flutter run

Confirmed by docs.flutter.dev

Did this fix it?

Cause 3 of 4

The deprecated onPopInvoked callback

1Check

An earlier PopScope callback was replaced by one that also carries the pop result.

grep -rn "onPopInvoked:" lib/

2Fix

Rename the argument to onPopInvokedWithResult and take the extra result parameter.

PopScope(
  canPop: true,
  onPopInvokedWithResult: (bool didPop, Object? result) {
    _saveDraft();
  },
  child: child,
)

3Verify

Analyze again.

flutter analyze

Confirmed by api.flutter.dev

Did this fix it?

Cause 4 of 4

Form.onWillPop and nested navigators

1Check

These two places have their own replacements.

grep -rn "onWillPop" lib/

2Fix

Form takes canPop and onPopInvokedWithResult, used like PopScope. For a nested Navigator, wrap it in NavigatorPopHandler.

NavigatorPopHandler(
  onPop: () => _nestedNavigatorKey.currentState!.pop(),
  child: Navigator(
    key: _nestedNavigatorKey,
    onGenerateRoute: _onGenerateRoute,
  ),
)

3Verify

Press back inside the nested flow. It pops the inner route first.

flutter run

Confirmed by docs.flutter.dev

Did this fix it?

How do I stop it coming back?

  • Run flutter analyze in CI so deprecations show up when you upgrade Flutter.
  • Decide canPop from state (for example unsaved changes) instead of asking at pop time.
  • Read the breaking-changes page before each Flutter upgrade.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.