DEV Community

Jehadur Rahman Emran
Jehadur Rahman Emran

Posted on

PopScope still fails on GoRouter root routes — a MethodChannel workaround

Starting with GoRouter v13+, PopScope does not reliably handle the Android back button on root routes. That limitation is documented in flutter/flutter#140869, which is now closed. Cousin issues such as #153546 and #112765 are worth reading if you are still hitting edge cases.

I published go_router_back_handler 1.0.2 on pub.dev under the jehadurre.me publisher to work around this. It intercepts the back button on the Android side through a MethodChannel, then decides whether to pop, walk a route hierarchy you give it, or show an exit confirmation on a root route.

The Dart side

GoRouterBackHandler.initialize(
  navigatorKey: rootNavigatorKey,
  routeHierarchy: {
    '/profile': '/home',
    '/settings': '/home',
    '/details': '/list',
  },
  rootRoutes: ['/', '/login', '/home'],
);
Enter fullscreen mode Exit fullscreen mode

Pass the same rootNavigatorKey into GoRouter. Call GoRouterBackHandler.dispose() in dispose(). A longer copy-pasteable snippet is in this gist and in the package README.

The native step is not optional

A Dart-only paste will not intercept the back button. You have to update MainActivity.kt.

  • The channel name must be exactly com.gorouter.back_handler/back_button.
  • After changing native code, do a full restart, not a hot reload.
  • The full Kotlin is in the package README. Copy it carefully and keep your own application id in the package line.

If the app exits immediately with no confirmation, the usual cause is a missing or mismatched MainActivity.kt.

What this post is not claiming

The live pub.dev score on 28 November 2026 was 140/160 (static analysis and outdated dependencies still cost points). I am not going to pretend it is 160/160. A v1.0.3 that closes that gap would be a later, approval-gated release.

Install:

dependencies:
  go_router_back_handler: ^1.0.2
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
userxnom profile image
Abdullah Naseer •

The 140/160 pub.dev score, with the honesty about what still costs points, is the detail that makes me trust the package — most workaround posts would round that up to "perfect". The part I would steal first is the routeHierarchy idea: encoding back behavior as data instead of scattering PopScope widgets is the kind of thing that survives the next GoRouter upgrade. One question: when the app is restored after Android kills it in the background, does the hierarchy need re-initializing, or does the MethodChannel side remember the routes on its own?

Collapse
 
jehadurre profile image
Jehadur Rahman Emran •

Thanks, really appreciate you reading it that closely!

Short answer: no extra step needed. The routeHierarchy lives only in Dart memory. The native side stores nothing; MainActivity just forwards each back press over the MethodChannel. When Android kills the process and you come back, a fresh Dart isolate starts, main() runs again, and the initialize() call in your root widget's initState() sets everything up again before any back press gets handled.

Which screen you land on is go_router's job (restorationScopeId if you want the location restored), but whatever route you land on, the same hierarchy applies.

Your question made me test this properly, and I found a related bug: dispose() left the channel handler registered, so back presses after it were silently swallowed. Fixed, with tests: github.com/JehadurRE/go_router_bac...