[go_router] relative navigation leads to error if user navigates quickly
- Dominant language
- Dart
- Stars
- 179k
- Forks
- 31.1k
- PR merge metrics
- PR metrics pending
Description
### Steps to reproduce
1. Run `flutter create`, replace the contents of the `main.dart` file with the sample provided
2. Replace the contents of the `pubspec.yaml` by the following:
```yaml
name: relative_route_bug_reproduction
description: "A new Flutter project."
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ^3.9.2
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.8
go_router: ^16.2.1
dev_dependencies:
build_runner: ^2.6.1
go_router_builder: 4.0.1
flutter_test:
sdk: flutter
flutter_lints: ^5.0.0
flutter:
uses-material-design: true
```
3. Run `flutter pub get`
4. Run `dart run build_runner build`
5. Run the app (I tested it on Android, Pixel 7 also shown in the output of `flutter doctor -v`)
6. Click on the two buttons in the app at the same time
### Expected results
Only one of the navigations is executed.
### Actual results
An attempt is made to navigate to both locations and the error page of go_router is shown:
### Code sample
Code sample
```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
part 'main.g.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
routerConfig: GoRouter(
debugLogDiagnostics: true,
routes: $appRoutes,
initialLocation: '/${NavigationRoutes.a}',
),
);
}
}
class SubPage extends StatelessWidget {
const SubPage({super.key, required this.location});
final String location;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(location),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Hello from $location',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
);
}
}
class APage extends StatelessWidget {
const APage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text('APage'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () {
BRoute().go(context);
},
child: Text('Navigate to b'),
),
ElevatedButton(
onPressed: () {
CRoute().goRelative(context);
},
child: Text('Navigate to c'),
),
],
),
),
);
}
}
class NavigationRoutes {
static const String a = "a";
static const String b = "b";
static const String c = "c";
}
const bRoute = TypedGoRoute(
name: NavigationRoutes.b,
path: NavigationRoutes.b,
);
class BRoute extends GoRouteData with $BRoute {
@override
Widget build(BuildContext context, GoRouterState state) {
return SubPage(location: "BRoute");
}
}
const cRoute = TypedRelativeGoRoute(path: NavigationRoutes.c);
class CRoute extends RelativeGoRouteData with $CRoute {
@override
Widget build(BuildContext context, GoRouterState state) {
return SubPage(location: "CRoute");
}
}
@TypedGoRoute(
name: NavigationRoutes.a,
path: '/${NavigationRoutes.a}',
routes: [bRoute, cRoute],
)
class ARoute extends GoRouteData with $ARoute {
@override
Widget build(BuildContext context, GoRouterState state) {
return APage();
}
}
```
### Screenshots or Video
Screenshots / Video demonstration
https://github.com/user-attachments/assets/e96f68d9-128d-48fa-a685-22c3a6af616a
### Logs
Logs
```console
[GoRouter] going to /a/b
[GoRouter] going to ./c
[GoRouter] No initial matches: /a/b/c
```
### Flutter Doctor output
Doctor output
```console
[✓] Flutter (Channel stable, 3.35.3, on macOS 15.6.1 24G90 darwin-arm64, locale de-DE) [338ms]
• Flutter version 3.35.3 on channel stable at /[...]/fvm/versions/3.35.3
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision a402d9a437 (2 weeks ago), 2025-09-03 14:54:31 -0700
• Engine revision ddf47dd3ff
• Dart version 3.9.2
• DevTools version 2.48.0
• Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations, enable-lldb-debugging
[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [2,2s]
• Android SDK at /[...]/Library/Android/sdk
• Emulator version 36.1.9.0 (build_id 13823996) (CL:N/A)
• Platform android-36, build-tools 36.0.0
• Java binary at: /[...]/Android Studio.app/Contents/jbr/Contents/Home/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.6+-13391695-b895.109)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 16.4) [918ms]
• Xcode at /[...]/Xcode.app/Contents/Developer
• Build 16F6
• CocoaPods version 1.16.2
[✓] Chrome - develop for the web [26ms]
• Chrome at /[...]/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2025.1) [26ms]
• 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 21.0.6+-13391695-b895.109)
[✓] IntelliJ IDEA Ultimate Edition (version 2025.1.1.1) [25ms]
• IntelliJ at /[...]/Applications/IntelliJ IDEA Ultimate.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.104.0) [6ms]
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.118.0
[✓] Connected device (3 available) [6,2s]
• Pixel 7 (mobile) • 2A311FDH20000T • android-arm64 • Android 16 (API 36)
• macOS (desktop) • macos • darwin-arm64 • macOS 15.6.1 24G90 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 140.0.7339.133
[✓] Network resources [204ms]
• All expected network resources are available.
• No issues found!
```
Contributor guide
Assessment
This issue has not been assessed yet.