[go_router][go_router_builder] It will be executed twice redirect when putting the URL directly
- Dominant language
- Dart
- Stars
- 179k
- Forks
- 31.1k
- PR merge metrics
- PR metrics pending
Description
### Steps to reproduce
1. run it on broswer: chrome
e.g. url is : `http://localhost:53306/`
2. put url in Put the url into the address bar.
e.g. `http://localhost:53306/login
3. It will run twice, then show root in page
e.g. `http://localhost:53306/`
### Expected results
Still in Login page
### Actual results
wait late or click other window, It will go back to home page.
### Code sample
main.dart
```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:web_app/router.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
usePathUrlStrategy();
runApp(const App());
}
class App extends StatelessWidget {
const App({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp.router(
debugShowCheckedModeBanner: false,
routerConfig: GoRouter(
navigatorKey: rootKey,
debugLogDiagnostics: true,
routes: $appRoutes,
initialLocation: "/",
redirect: (context, state) {
print("_>___ app: get router.base path: ${Uri.base.path}/base: ${Uri.base}");
print("__>__ app: get router.location: ${state.matchedLocation}");
final uri = Uri.parse(state.matchedLocation);
final paths = [ContentRouteData().location];
if (paths.contains(uri.path)) {
return LoginRouteData().location;
}
return null;
},
errorBuilder: (context, state) {
// return ErrorRoute().build(context, state);
return Container();
},
// refreshListenable: _appProvider,
),
);
}
}
class IndexPage extends StatelessWidget {
final Widget child;
const IndexPage({super.key, required this.child});
@override
Widget build(BuildContext context) {
return child;
}
}
class HomePage extends StatelessWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
const Text("Home Page"),
ElevatedButton(
onPressed: () {
context.go(ContentRouteData().location);
},
child: const Text("go content"),
),
],
),
),
);
}
}
class MemberPage extends StatelessWidget {
const MemberPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text("Member Page"),
),
);
}
}
class ContentPage extends StatelessWidget {
const ContentPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text("Content Page"),
),
);
}
}
class LoginPage extends StatelessWidget {
const LoginPage({super.key});
@override
Widget build(BuildContext context) {
return const Scaffold(
body: Center(
child: Text("Login Page"),
),
);
}
}
```
router.dart
```dart
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:web_app/main.dart';
part 'router.g.dart';
final rootKey = GlobalKey();
final shellKey = GlobalKey(debugLabel: "index");
@TypedShellRoute(routes: [
TypedGoRoute(path: "/", routes: [
// TypedGoRoute(path: "login"),
]),
TypedGoRoute(path: "/content"),
])
class IndexRouteData extends ShellRouteData {
static final GlobalKey $parentNavigatorKey = rootKey;
static final GlobalKey $navigatorKey = shellKey;
@override
Page pageBuilder(BuildContext context, GoRouterState state, Widget navigator) {
return NoTransitionPage(child: IndexPage(child: navigator));
}
}
class HomeRouteData extends GoRouteData {
@override
Page buildPage(BuildContext context, GoRouterState state) {
return const NoTransitionPage(
child: HomePage(),
);
}
}
class ContentRouteData extends GoRouteData {
@override
Page buildPage(BuildContext context, GoRouterState state) {
return const NoTransitionPage(
child: ContentPage(),
);
}
}
@TypedGoRoute(path: "/login")
class LoginRouteData extends GoRouteData {
// static final GlobalKey $parentNavigatorKey = rootKey;
@override
Page buildPage(BuildContext context, GoRouterState state) {
return const NoTransitionPage(
child: LoginPage(),
);
}
}
```
pubspec.yaml
```yaml
name: web_app
description: "A new Flutter project."
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ^3.5.3
dependencies:
flutter:
sdk: flutter
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
go_router: ^14.4.1
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^4.0.0
build_runner: ^2.4.11
go_router_builder: ^2.7.0
flutter:
uses-material-design: true
```
### Screenshots or Video
Screenshots / Video demonstration
https://github.com/user-attachments/assets/bf04c873-a751-4005-8f10-573a7243bc41
### Logs
Logs
```console
_>___ app: get router.base path: /login/base: http://localhost:53306/login
__>__ app: get router.location: /login
[GoRouter] Full paths for routes:
├─ (ShellRoute)
│ ├─/ (Widget)
│ └─/content (Widget)
└─/login (Widget)
[GoRouter] setting initial location /
_>___ app: get router.base path: /login/base: http://localhost:53306/login
__>__ app: get router.location: /
[GoRouter] Full paths for routes:
├─ (ShellRoute)
│ ├─/ (Widget)
│ └─/content (Widget)
└─/login (Widget)
[GoRouter] setting initial location /
```
### Flutter Doctor output
Doctor output
```console
flutter -v doctor
[✓] Flutter (Channel stable, 3.24.3, on macOS 14.6.1 23G93 darwin-arm64, locale zh-Hant-TW)
• Flutter version 3.24.3 on channel stable at /Users/mosil/development/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 2663184aa7 (8 weeks ago), 2024-09-11 16:27:48 -0500
• Engine revision 36335019a8
• Dart version 3.5.3
• DevTools version 2.37.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Connected device (5 available)
• Chrome (web) • chrome • web-javascript • Google Chrome 130.0.6723.92
```
Contributor guide
Assessment
This issue has not been assessed yet.