flutter / flutter/flutter

[go_router] Updating the GoRouter.routeConfig loses the router state

Open
#167,344 9 comments 1 reaction 0 assignees View on GitHub
found in release: 3.29 found in release: 3.32 has reproducible steps p: go_router P2 package team-ecosystem triaged-ecosystem
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

### Steps to reproduce

Reproducible in go_router `15.0.0`

Start with the following config:

```dart
final config = RoutingConfig(routes: [
StatefulShellRoute.indexedStack(
restorationScopeId: 'navigationShell',
builder: builder,
branches: [
StatefulShellBranch(
restorationScopeId: 'home',
routes: [
GoRoute(path: '/home', builder: homeBuilder, routes: [
GoRoute(path: '/a', builder: ABuilder)
])
]
),
],
),
]);
```

and add a button on ABuilder to update the routing config:

```dart
configNotifier.value = RoutingConfig // Same value as above
```

1. From the home branch, navigate to `/home/a` using the push method ( imperative ). The issue doesn't happen with the go method.
2. Click the reload button on the A widget
3. Observe that the reload redirected the user back to `/home`

### Expected results

Update route config, stay in the route: `/home/a`, and the history of the branch should be maintained

### Actual results

Route config is updated but the history of the branch is lost, and the app redirected to `/home`

### Code sample

Code sample

```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
runApp(const App());
}

class App extends StatelessWidget {
static final routingConfig = ValueNotifier(createConfig());
static final routerConfig = GoRouter.routingConfig(
restorationScopeId: 'app-router',
initialLocation: '/home',
routingConfig: routingConfig,
);

const App({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp.router(
restorationScopeId: 'app',
routerConfig: routerConfig,
);
}
}

Widget builder(
BuildContext context,
GoRouterState state,
StatefulNavigationShell shell,
) {
return Scaffold(
bottomNavigationBar: NavigationBar(
selectedIndex: shell.currentIndex,
onDestinationSelected: (index) => shell.goBranch(index),
destinations: [
NavigationDestination(icon: const Icon(Icons.abc), label: 'Main'),
NavigationDestination(icon: const Icon(Icons.abc), label: 'Ignore'),
],
),
body: Center(child: shell),
);
}

RoutingConfig createConfig() {
return RoutingConfig(
routes: [
StatefulShellRoute.indexedStack(
restorationScopeId: 'navigationShell',
builder: builder,
branches: [
StatefulShellBranch(
restorationScopeId: 'main',
routes: [
GoRoute(
path: '/home',
builder: createBuilder('main', canNavigate: true),
routes: [
GoRoute(
path: '/a',
builder: createBuilder('a', allowReload: true),
),
],
),
],
),
StatefulShellBranch(
restorationScopeId: 'ignore',
routes: [
GoRoute(
path: '/ignore',
builder: createBuilder('ignore', canNavigate: true),
),
],
),
],
),
],
);
}

Widget Function(BuildContext, GoRouterState) createBuilder(
String name, {
bool allowReload = false,
bool canNavigate = false,
}) {
return (ctx, state) =>
MyPage(allowReload: allowReload, canNavigate: canNavigate, name: name);
}

class MyPage extends StatelessWidget {
final bool allowReload;
final bool canNavigate;
final String name;

const MyPage({
super.key,
required this.allowReload,
required this.canNavigate,
required this.name,
});

@override
Widget build(BuildContext context) {
return Material(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (allowReload)
ElevatedButton(
onPressed: () {
App.routingConfig.value = createConfig();
},
child: Text("Press me to reload config"),
),
if (canNavigate)
ElevatedButton(
onPressed: () {
GoRouter.of(context).push('/home/a');
},
child: Text("Press me to navigate to nested route"),
),
Text(name),
],
),
),
);
}
}
```

### Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

### Logs

Logs

```console
[Paste your logs here]
```

### Flutter Doctor output

Doctor output

```console
[√] Flutter (Channel stable, 3.29.1, on Microsoft Windows [Version 10.0.26100.3775], locale el-GR) [381ms]
• Flutter version 3.29.1 on channel stable at C:\Users\itsUndefined\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 09de023485 (7 weeks ago), 2025-02-28 13:44:05 -0800
• Engine revision 871f65ac1b
• Dart version 3.7.0
• DevTools version 2.42.2

[√] Windows Version (11 Pro 64-bit, 24H2, 2009) [1.989ms]

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.1) [2,9s]
• Android SDK at C:\Users\itsUndefined\AppData\Local\Android\Sdk
• Platform android-35, build-tools 35.0.1
• ANDROID_HOME = C:\Users\itsUndefined\AppData\Local\Android\Sdk
• Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java
This is the JDK bundled with the latest Android Studio installation on this machine.
To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
• Java version OpenJDK Runtime Environment (build 21.0.5+-13047016-b750.29)
• All Android licenses accepted.

[√] Chrome - develop for the web [120ms]
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.13.6) [118ms]
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
• Visual Studio Community 2022 version 17.13.35931.197
• Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2024.2) [16ms]
• Android Studio at C:\Program Files\Android\Android Studio
• 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 21.0.5+-13047016-b750.29)

[√] VS Code (version 1.99.2) [14ms]
• VS Code at C:\Users\itsUndefined\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.108.0

[√] Connected device (4 available) [268ms]
• sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64 • Android 15 (API 35) (emulator)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.26100.3775]
• Chrome (web) • chrome • web-javascript • Google Chrome 135.0.7049.85
• Edge (web) • edge • web-javascript • Microsoft Edge 135.0.3179.73

[√] Network resources [341ms]
• All expected network resources are available.

• No issues found!
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.