Repository navigation
[go_router] GoRouterState incorrect for ShellRoute on .push and .pop #125752
Description
Activity
- addedin triagePresently being triaged by the triage teamPresently being triaged by the triage team
on May 2, 2023 Thanks for the report. Seeing the same behavior as reported.
stable, master flutter doctor -v
[!] Flutter (Channel stable, 3.7.12, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.7.12 on channel stable at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 4d9e56e694 (3 days ago), 2023-04-17 21:47:46 -0400 • Engine revision 1a65d409c7 • Dart version 2.19.6 • DevTools version 2.20.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category. [!] Flutter (Channel master, 3.10.0-16.0.pre.23, on macOS 12.2.1 21D62 darwin-x64, locale en-GB) • Flutter version 3.10.0-16.0.pre.23 on channel master at /Users/dhs/documents/fluttersdk/flutter ! Warning: `flutter` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. ! Warning: `dart` on your path resolves to /Users/dhs/Documents/Fluttersdk/flutter/bin/dart, which is not inside your current Flutter SDK checkout at /Users/dhs/documents/fluttersdk/flutter. Consider adding /Users/dhs/documents/fluttersdk/flutter/bin to the front of your path. • Upstream repository https://github.com/flutter/flutter.git • Framework revision 55c988fb45 (4 hours ago), 2023-04-27 20:32:55 -0400 • Engine revision 1d25906b4c • Dart version 3.1.0 (build 3.1.0-49.0.dev) • DevTools version 2.23.1 • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades. [!] Android toolchain - develop for Android devices (Android SDK version 30.0.3) • Android SDK at /Users/dhs/Library/Android/sdk ✗ cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 13.2.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 13C100 • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] IntelliJ IDEA Ultimate Edition (version 2021.3.2) • IntelliJ at /Applications/IntelliJ IDEA.app • Flutter plugin version 65.1.4 • Dart plugin version 213.7228 [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.29.0 [✓] Connected device (3 available) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 15.3.1 19D52 • macOS (desktop) • macos • darwin-x64 • macOS 12.2.1 21D62 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 109.0.5414.119 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. [!] Xcode - develop for iOS and macOS (Xcode 12.3) • Xcode at /Applications/Xcode.app/Contents/Developer ! Flutter recommends a minimum Xcode version of 13. Download the latest version or update via the Mac App Store. • CocoaPods version 1.11.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] VS Code (version 1.62.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.21.0 [✓] Connected device (5 available) • SM G975F (mobile) • RZ8M802WY0X • android-arm64 • Android 11 (API 30) • Darshan's iphone (mobile) • 21150b119064aecc249dfcfe05e259197461ce23 • ios • iOS 14.4.1 18D61 • iPhone 12 Pro Max (mobile) • A5473606-0213-4FD8-BA16-553433949729 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-3 (simulator) • macOS (desktop) • macos • darwin-x64 • Mac OS X 10.15.4 19E2269 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 98.0.4758.80 [✓] HTTP Host Availability • All required HTTP hosts are available ! Doctor found issues in 1 category.- addedpackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: go_routerThe go_router packageThe go_router packagefound in release: 3.7Found to occur in 3.7Found to occur in 3.7found in release: 3.10Found to occur in 3.10Found to occur in 3.10and removedin triagePresently being triaged by the triage teamPresently being triaged by the triage team
on May 2, 2023 - addedP2Important issues not at the top of the work listImportant issues not at the top of the work list
on May 2, 2023 Updated the code sample to fix the breaking changes in the last couple of go_router releases. Currently seeing the issue on go_router 9.0.3
Updated code sample
import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; void main() { runApp(MyApp()); } final shellKey = GlobalKey<NavigatorState>(); class MyApp extends StatelessWidget { MyApp({Key? key}) : super(key: key); final _router = GoRouter( initialLocation: '/', debugLogDiagnostics: true, routes: [ ShellRoute( navigatorKey: shellKey, builder: (context, state, child) => ShellScaffold(child: child), pageBuilder: (context, state, child) => MaterialPage(child: ShellScaffold(child: child)), routes: [ GoRoute( name: 'Home', path: '/', builder: (context, state) => const Home(), routes: [ GoRoute( name: 'Page A', path: 'a', builder: (context, state) => const PageA(), routes: [ GoRoute( name: 'Page B', path: 'b', builder: (context, state) => const PageB(), routes: [ GoRoute( name: 'Page C', path: ':id', builder: (context, state) => PageCWithParam( id: state.pathParameters['id']!, ), ), ], ), ], ), ], ), ], ) ], ); @override Widget build(BuildContext context) { return MaterialApp.router( title: 'Shell Route State Test', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), routerConfig: _router, ); } } class ShellScaffold extends StatelessWidget { const ShellScaffold({Key? key, required this.child}) : super(key: key); final Widget child; @override Widget build(BuildContext context) { final state = GoRouterState.of(context); ///Below is a workaround after the 9.0.0 changes that remove GoRouter.location ///This can be used to get the actual location when pushing a route onto the ShellRoute. ///GoRouterState is incomplete for pushed/imperative routes as of 9.0.3 GoRouter router = GoRouter.of(context); final RouteMatch lastMatch = router.routerDelegate.currentConfiguration.last; final RouteMatchList matchList = lastMatch is ImperativeRouteMatch ? lastMatch.matches : router.routerDelegate.currentConfiguration; final String location = matchList.uri.toString(); print('ShellScaffold build'); print('location: $location'); print('GoRouterState location: ${state.location}'); print('GoRouterState matchedLocation: ${state.matchedLocation}'); print('GoRouterState fullPath: ${state.fullPath}'); print('GoRouterState pathParameters: ${state.pathParameters}'); return Scaffold( appBar: AppBar( title: Text(state.location), // automaticallyImplyLeading: true, /// automaticallyImplyLeading does not seem work inside of a ShellRoute. /// Manually defining leading below is then required to show a back button. /// This is why having a correct GoRouterState is needed. leading: (state.location.startsWith('/a')) ? BackButton( onPressed: () => context.pop(), ) : null, actions: [ /// Used to reset the state of the current path and return to home. IconButton( onPressed: () => context.go('/'), icon: const Icon(Icons.home)) ], ), body: child, ); } } class Home extends StatelessWidget { const Home({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( children: [ const Text('Home'), TextButton( onPressed: () => context.go('/a'), child: const Text('Go /a'), ), TextButton( onPressed: () => context.push('/a'), child: const Text('Push /a'), ), ], ), ), ); } } class PageA extends StatelessWidget { const PageA({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Column( children: [ const Text('Page A'), TextButton( onPressed: () => context.go('/a/b'), child: const Text('Go /a/b'), ), TextButton( onPressed: () => context.push('/a/b'), child: const Text('Push /a/b'), ), TextButton( onPressed: () => context.pop(), child: const Text('pop'), ), ], ), ), ); } } class PageB extends StatelessWidget { const PageB({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return Center( child: Column( children: [ const Text('Page B'), TextButton( onPressed: () => context.go('/a/b/1'), child: const Text('Go /a/b/1'), ), TextButton( onPressed: () => context.push('/a/b/2'), child: const Text('Push /a/b/2'), ), TextButton( onPressed: () => context.pop(), child: const Text('pop'), ), ], ), ); } } class PageCWithParam extends StatelessWidget { const PageCWithParam({Key? key, required this.id}) : super(key: key); final String id; @override Widget build(BuildContext context) { return Center( child: Column( children: [ Text('Page C with param: $id'), TextButton( onPressed: () => context.pop(), child: const Text('pop'), ), ], ), ); } }
Piling onto that. Having two separate
ShellRoutes, we are seeing the wrong ShellRoute being used when usingpush- but it is all fine usinggo.Example Code (against 10.1.2, 11.1.0)
```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';final GlobalKey rootNavKey = GlobalKey(
debugLabel: 'root',
);class MyApp extends StatelessWidget {
const MyApp({super.key});@OverRide
Widget build(BuildContext context) {
return MaterialApp.router(
title: 'Flutter Demo',
routerConfig: GoRouter(
initialLocation: '/a',
routes: [
ShellRoute( // App Base
pageBuilder: (context, state, child) {
return NoTransitionPage(
key: state.pageKey,
child: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('App Route'),
Expanded(child: child),
],
),
),);
},
routes: [
GoRoute(
path: '/c',
pageBuilder: (context, state) {
return NoTransitionPage(
key: state.pageKey,
child: const Text("other"),
);
},
),
ShellRoute(
pageBuilder: (context, state, child) {
return NoTransitionPage(
key: state.pageKey,
child: Scaffold(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('ShellRoute header A:'),
Expanded(child: child),
],
),
),);
},
routes: [
GoRoute(
path: '/a',
name: 'aIndex',
builder: (context, state) => Column(children: [Text('a: index'),
OutlinedButton(child: Text('go to a'), onPressed: () => context.goNamed('aIndex')),
OutlinedButton(child: Text('go to b:details'), onPressed: () => context.goNamed('bDetails', pathParameters: {'roomId': '1244'})),
OutlinedButton(child: Text('push to a:details'), onPressed: () => context.pushNamed('aDetails')),
OutlinedButton(child: Text('push to b:details'), onPressed: () => context.pushNamed('bDetails', pathParameters: {'roomId': '1244'}))]),
),
GoRoute(
path: '/a/details',
name: 'aDetails',
builder: (context, state) => Column(children: [Text('a: details'),
OutlinedButton(child: Text('go to a'), onPressed: () => context.goNamed('aIndex')),
OutlinedButton(child: Text('go to b:details'), onPressed: () => context.goNamed('bDetails', pathParameters: {'roomId': '1244'})),
OutlinedButton(child: Text('push to a'), onPressed: () => context.pushNamed('aIndex')),
OutlinedButton(child: Text('push to b:details'), onPressed: () => context.pushNamed('bDetails', pathParameters: {'roomId': '1244'}))]),
),
],
),ShellRoute( pageBuilder: (context, state, child) { return NoTransitionPage( key: state.pageKey, child:Scaffold( body: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text('ShellRoute header B:'), Expanded(child: child), ], ),), ); }, routes: [ GoRoute( name: 'bIndex', path: '/b', builder: (context, state) => Text('b: index'), ), GoRoute( name: 'bDetails', path: '/b/:roomId/details', builder: (context, state) => Column(children: [Text('b: details'), OutlinedButton(child: Text('push to a'), onPressed: () => context.pushNamed('aIndex')), OutlinedButton(child: Text('push to b'), onPressed: () => context.pushNamed('bIndex'))]), ), ], ) ], ), ],), );}
}void main() async {
runApp(MyApp());
}</p> </details>- added a commit that references this issue
on Dec 21, 2023 - addedr: fixedIssue is closed as already fixed in a newer versionIssue is closed as already fixed in a newer version
on Jan 2, 2024 This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.- locked as resolved and limited conversation to collaborators
on Jan 16, 2024 - added a commit that references this issue
on Jun 10, 2026 - added a commit that references this issue
on Jun 19, 2026
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Is there an existing issue for this?
Steps to reproduce
Expected results
Expect context.go(), context.push(), context.pop(), to update GoRouterState in a ShellRoute correctly.
Currently it looks like:
context.go() updates GoRouterState.
context.pop() from a context.go() partially updates GoRouterState(seems to only update .location).
context.push() does NOT update GoRouterState.
context.pop() from a context.push() does NOT update GoRouterState.
A correct GoRouterState in a shell route builder is especially important if you want to display an AppBar in the shell route builder that uses GoRouterState to update the AppBar title or dynamically show a navigation drawer or back button. (Like I show in the sample code)
Another use case would be to grab a param from the GoRouterState to load some logic or state in the shell route builder widget and pass it to a state management solution like Bloc, Inherited Widget or Riverpod ProviderScope to make it available to all child widgets.
I tested this on iOS and Android.
I do not have much experience using queryParams or extra so I did not check if they are updated correctly in GoRouterState during my testing.
Also GoRouterState.name always shows up at null in shell routes and I believe from previous discussions on here that is expected behavior as ShellRoutes don’t have a name parameter. I think that is understandable but I would expect the ShellRoute’s GoRouterState location, subloc, fullPath, params to be updated correctly and with all the params available from the current path.
Actual results
Code sample
Code sample
Flutter Doctor output
Doctor output