Repository navigation
[go_router] PopScope seems to be incompatible with GoRouter #138737
Description
Is there an existing issue for this?
- I have searched the existing issues
- I have read the guide to filing a bug
Steps to reproduce
Original comment was made at #138525 (comment) but I'm pasting here:
[...] our team was investigating PopScope and found some interesting behavior that can be reproduced using this repo. I don't believe this component is behaving as described in the docs.
Using GoRouter APIs on iOS 17:
- When
canPopis set totrue, user can go back via app bar or back button butonPopInvokedis never invoked. - When
canPopis set tofalse, user cannot go back via app bar or back button but againonPopInvokedis only invoked withdidPopset tofalsewhen pressing app bar back button. It is not invoked for the back gesture.
Using GoRouter APIs on Android 14:
- When
canPopis set totrue, user can go back via app bar or back button butonPopInvokedis never invoked. - When
canPopis set tofalse, user cannot go back via app bar or back button.onPopInvokedis invoked in both cases withdidPopset tofalse.
Summary Table:
NOTE: For clarity, please note that for each column with onPopInvoked in the title, the true or false in the rows associated with that column indicate whether or not the onPopInvoked callback was invoked. This column is NOT intended to indicate anything about the didPop value, which is always (AFAIK) correct.
| Platform | Nav API | canPop |
onPopInvoked(appBar) |
onPopInvoked (back swipe) |
onPopInvoked (Physical button) |
Expected behavior? |
|---|---|---|---|---|---|---|
| iOS | GoRouter | true | false | false | --- | false |
| iOS | GoRouter | false | true | false | --- | false |
| Android | GoRouter | true | false | false | false | false |
| Android | GoRouter | false | true | true | true | true |
Expected results
The component should behave as described in the docs.
Actual results
See the table in the Steps to reproduce section.
Code sample
Code can be found in this repo.
Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]Flutter Doctor output
Doctor output
[✓] Flutter (Channel stable, 3.16.0, on macOS 13.6.1 22G313 darwin-arm64, locale en-US)
• Flutter version 3.16.0 on channel stable at /Users/brandontrautmann/fvm/versions/3.16.0
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision db7ef5bf9f (2 days ago), 2023-11-15 11:25:44 -0800
• Engine revision 74d16627b9
• Dart version 3.2.0
• DevTools version 2.28.2
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
• Android SDK at /Users/brandontrautmann/Library/Android/sdk
• Platform android-33, build-tools 33.0.1
• ANDROID_SDK_ROOT = /Users/brandontrautmann/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
• Xcode at /Applications/Xcode_14.app/Contents/Developer
• Build 14E300c
• CocoaPods version 1.12.1
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2022.3)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
[✓] IntelliJ IDEA Community Edition (version 2023.2.1)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
[✓] VS Code (version 1.84.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.76.0
[✓] Connected device (4 available)
• sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 13 (API 33) (emulator)
• iPhone 13 mini (mobile) • 0492C672-A151-402C-AAA0-0B694038CF37 • ios • com.apple.CoreSimulator.SimRuntime.iOS-17-0 (simulator)
• macOS (desktop) • macos • darwin-arm64 • macOS 13.6.1 22G313 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 119.0.6045.159
[✓] Network resources
• All expected network resources are available.
• No issues found!
Activity
dam-ease commentedon Nov 21, 2023
Thanks for filing this and the detailed explanation.
I can reproduce this on the latest
masterandstablechannels.stable, master flutter doctor -v
chunhtai commentedon Nov 30, 2023
I am actively working on this. It will have to wait for #137792 before I can fix this issue
RioChndr commentedon Dec 24, 2023
Hi @btrautmann. I look into your reproduce_repo. I see you use
goinsteadpush, I wonder why you use this method. It would be nice if popScope could trigger theonPopInvoked. I also create Reproduce Repo that usegoandpushto test it.btrautmann commentedon Dec 26, 2023
Our team uses
gopretty much everywhere because it's idempotent (i.e you can invoke agocall 100x and you will have the same result as if you invoked it just once). This is not true ofpushand can lead to navigation issues. There are obviously use cases forpushbut IMO it shouldn't be the default.Edit: Just to clarify, my usage of
goin the repro was simply habit, I didn't intend it to influence the filed issue behavior at all.95 remaining items
Gustl22 commentedon Jun 19, 2025
go_router with the fix was released in >=15.2.3.
Is anyone experiencing further issues? Otherwise this probably can be closed.
Gustl22 commentedon Jun 30, 2025
@btrautmann I guess this can be closed, as its fixed ^^
juampiq6 commentedon Jul 1, 2025
@Gustl22 i was investigating related issues and ran into this one, can my issue #171451 have something to do with this one?
Edit: I already tried with latests 15.2.4 version and my issue can still be reproduced
[-]PopScope seems to be incompatible with GoRouter[/-][+][go_router] PopScope seems to be incompatible with GoRouter[/+]TheManuz commentedon Oct 2, 2025
Yep, sadly GoRouter doesn't respect
PopScopewidget withcanPop: false.The result is that
onPopInvokedWithResultwill be called (as expected) with a truedidPop(expected false).Tried this on
go_router: 16.2.1EDIT: tested on
go_router: 15.1.3, same behaviorkerimamansaryyev commentedon Dec 17, 2025
@Gustl22 @chunhtai
I investigated behavior of
PopScopethat is used in routes under StatefulShellRoute, or a widget wrapping the navigation container of the StatefulShellRoute. I intended to implement a use-case where the app is prevented from closing when the tab view (the indexed stack incorporated byStatefulShellRoute.indexedStack) was focused on any tab except the Home tab (at index 0), and a user would be navigated to the Home tab afterwards.I wrapped a widget (the shell) containing the tab view with
PopScopeand assigned the logic, however, behavior of thePopScopewas very flaky, sometimes it would work, sometimes the app would be closed. Upon investigation, I found out that the flaky behavior ofPopScopeis correlated with the Android Predictive Back feature. When I turned that off by addingandroid:enableOnBackInvokedCallback="false"into the application tag ofAndroidManifest.xml,PopScopedid indeed prevent closing the app.From my understanding, although the predictive back feature requires
canPopto be available ahead of time, the feature is not supposed to ignore it, is it? Because otherwise it wouldn't let developers reliably implement navigation prevention mechanisms like, for example, confirmation dialogs.Gustl22 commentedon Dec 17, 2025
@kerimamansaryyev I personally also experienced problems with predictive back gesture like in #152323. But that exceeds the scope of this issue. We ended up disabling it (until it works).