Ensure that initialLocation doesn't take precedence over deep-links (#2275)
* Ensure that initialLocation doesn't take precedence over deep-links
* Increment go_router patch version to 4.0.2
* Update CHANGELOG
diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md
index 41c3688..562c74a 100644
--- a/packages/go_router/CHANGELOG.md
+++ b/packages/go_router/CHANGELOG.md
@@ -1,6 +1,10 @@
+## 4.0.2
+
+- Fixes a bug where initialLocation took precedence over deep-links
+
## 4.0.1
-- Fix a bug where calling setLogging(false) does not clear listeners
+- Fixes a bug where calling setLogging(false) does not clear listeners.
## 4.0.0
diff --git a/packages/go_router/lib/src/go_router.dart b/packages/go_router/lib/src/go_router.dart
index 2fe633b..7afc5fa 100644
--- a/packages/go_router/lib/src/go_router.dart
+++ b/packages/go_router/lib/src/go_router.dart
@@ -46,9 +46,6 @@
setLogging(enabled: debugLogDiagnostics);
WidgetsFlutterBinding.ensureInitialized();
- final String _effectiveInitialLocation = initialLocation ??
- // ignore: unnecessary_non_null_assertion
- WidgetsBinding.instance.platformDispatcher.defaultRouteName;
routeInformationParser = GoRouteInformationParser(
routes: routes,
topRedirect: redirect ?? (_) => null,
@@ -56,8 +53,8 @@
debugRequireGoRouteInformationProvider: true,
);
routeInformationProvider = GoRouteInformationProvider(
- initialRouteInformation:
- RouteInformation(location: _effectiveInitialLocation),
+ initialRouteInformation: RouteInformation(
+ location: _effectiveInitialLocation(initialLocation)),
refreshListenable: refreshListenable);
routerDelegate = GoRouterDelegate(
@@ -213,4 +210,16 @@
routerDelegate.dispose();
super.dispose();
}
+
+ String _effectiveInitialLocation(String? initialLocation) {
+ final String platformDefault =
+ WidgetsBinding.instance.platformDispatcher.defaultRouteName;
+ if (initialLocation == null) {
+ return platformDefault;
+ } else if (platformDefault == '/') {
+ return initialLocation;
+ } else {
+ return platformDefault;
+ }
+ }
}
diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml
index 5fbb67f..cf9c4ef 100644
--- a/packages/go_router/pubspec.yaml
+++ b/packages/go_router/pubspec.yaml
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
-version: 4.0.1
+version: 4.0.2
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart
index ff915d0..bd7e3e8 100644
--- a/packages/go_router/test/go_router_test.dart
+++ b/packages/go_router/test/go_router_test.dart
@@ -1212,6 +1212,37 @@
);
expect(router.location, '/');
});
+
+ testWidgets(
+ 'does not take precedence over platformDispatcher.defaultRouteName',
+ (WidgetTester tester) async {
+ TestWidgetsFlutterBinding
+ .instance.platformDispatcher.defaultRouteNameTestValue = '/dummy';
+
+ final List<GoRoute> routes = <GoRoute>[
+ GoRoute(
+ path: '/',
+ builder: (BuildContext context, GoRouterState state) =>
+ const HomeScreen(),
+ routes: <GoRoute>[
+ GoRoute(
+ path: 'dummy',
+ builder: (BuildContext context, GoRouterState state) =>
+ const DummyScreen(),
+ ),
+ ],
+ ),
+ ];
+
+ final GoRouter router = await _router(
+ routes,
+ tester,
+ initialLocation: '/',
+ );
+ expect(router.routeInformationProvider.value.location, '/dummy');
+ TestWidgetsFlutterBinding
+ .instance.platformDispatcher.defaultRouteNameTestValue = '/';
+ });
});
group('params', () {