From 1f2a25bd53e8cb8891936e42e2527cb6d1b87cbd Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Wed, 16 Aug 2023 00:40:13 +0530 Subject: [PATCH 01/18] feat: override platform route Added optional parameter to override platform route mentioned in `MainActivity.kt`. --- packages/go_router/lib/src/router.dart | 17 +++++++++++++++++ packages/go_router/pubspec.yaml | 2 +- packages/go_router/test/go_router_test.dart | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index a5671e2fc9d..f5d06e1e84b 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -80,6 +80,7 @@ class GoRouter implements RouterConfig { int redirectLimit = 5, bool routerNeglect = false, String? initialLocation, + this.overridePlatformDefaultLocation = false, Object? initialExtra, List? observers, bool debugLogDiagnostics = false, @@ -91,6 +92,10 @@ class GoRouter implements RouterConfig { initialExtra == null || initialLocation != null, 'initialLocation must be set in order to use initialExtra', ), + assert( + overridePlatformDefaultLocation ? initialLocation != null : true, + 'Initial location must be set to override platform default' + ), assert( (onException == null ? 0 : 1) + (errorPageBuilder == null ? 0 : 1) + @@ -299,6 +304,13 @@ class GoRouter implements RouterConfig { @override late final GoRouteInformationParser routeInformationParser; + ///Defaults to [false], used to override platform default route mentioned + ///in MainActivity.kt. + ///If set to false, platform route will be used over `initialLocation` + ///set in `GoRouter` initialization. + ///See https://github.com/flutter/flutter/issues/132557 + final bool overridePlatformDefaultLocation; + /// Returns `true` if there is at least two or more route can be pop. bool canPop() => routerDelegate.canPop(); @@ -509,6 +521,11 @@ class GoRouter implements RouterConfig { String _effectiveInitialLocation(String? initialLocation) { final String platformDefault = WidgetsBinding.instance.platformDispatcher.defaultRouteName; + if(overridePlatformDefaultLocation) { + ///can force null check as it's already verified by + ///asset() while initialization + return initialLocation!; + } if (initialLocation == null) { return platformDefault; } else if (platformDefault == '/') { diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 3ad93e7ee93..c99ae043846 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: 10.1.0 +version: 10.1.1 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 e840b00cbd7..90b231b70e3 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5,6 +5,7 @@ // ignore_for_file: cascade_invocations, diagnostic_describe_all_properties, unawaited_futures import 'dart:async'; +import 'dart:ui'; import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; @@ -4863,6 +4864,25 @@ void main() { expect(statefulWidgetKeyB.currentState?.counter, equals(1)); }); }); + + ///Regression tests for https://github.com/flutter/flutter/issues/132557 + group('overridePlatformDefaultLocation', (){ + test('No initial location provided', () { + expect( () => GoRouter( + overridePlatformDefaultLocation: true, + routes: [ + GoRoute( + path: '/a', + builder: (BuildContext context, GoRouterState state) => const Placeholder(), + ), + GoRoute( + path: '/b', + builder: (BuildContext context, GoRouterState state) => const Placeholder(), + ), + ], + ), throwsA(const TypeMatcher())); + }); + }); } class TestInheritedNotifier extends InheritedNotifier> { From a334faecac8824cd95131fb2c00a5f06c676e369 Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Wed, 16 Aug 2023 00:44:01 +0530 Subject: [PATCH 02/18] feat: override platform route Added optional parameter to override platform route mentioned in `MainActivity.kt`. --- packages/go_router/lib/src/router.dart | 17 +++++++++++++++++ packages/go_router/pubspec.yaml | 2 +- packages/go_router/test/go_router_test.dart | 20 ++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index a5671e2fc9d..f5d06e1e84b 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -80,6 +80,7 @@ class GoRouter implements RouterConfig { int redirectLimit = 5, bool routerNeglect = false, String? initialLocation, + this.overridePlatformDefaultLocation = false, Object? initialExtra, List? observers, bool debugLogDiagnostics = false, @@ -91,6 +92,10 @@ class GoRouter implements RouterConfig { initialExtra == null || initialLocation != null, 'initialLocation must be set in order to use initialExtra', ), + assert( + overridePlatformDefaultLocation ? initialLocation != null : true, + 'Initial location must be set to override platform default' + ), assert( (onException == null ? 0 : 1) + (errorPageBuilder == null ? 0 : 1) + @@ -299,6 +304,13 @@ class GoRouter implements RouterConfig { @override late final GoRouteInformationParser routeInformationParser; + ///Defaults to [false], used to override platform default route mentioned + ///in MainActivity.kt. + ///If set to false, platform route will be used over `initialLocation` + ///set in `GoRouter` initialization. + ///See https://github.com/flutter/flutter/issues/132557 + final bool overridePlatformDefaultLocation; + /// Returns `true` if there is at least two or more route can be pop. bool canPop() => routerDelegate.canPop(); @@ -509,6 +521,11 @@ class GoRouter implements RouterConfig { String _effectiveInitialLocation(String? initialLocation) { final String platformDefault = WidgetsBinding.instance.platformDispatcher.defaultRouteName; + if(overridePlatformDefaultLocation) { + ///can force null check as it's already verified by + ///asset() while initialization + return initialLocation!; + } if (initialLocation == null) { return platformDefault; } else if (platformDefault == '/') { diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 3ad93e7ee93..c99ae043846 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: 10.1.0 +version: 10.1.1 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 e840b00cbd7..90b231b70e3 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5,6 +5,7 @@ // ignore_for_file: cascade_invocations, diagnostic_describe_all_properties, unawaited_futures import 'dart:async'; +import 'dart:ui'; import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; @@ -4863,6 +4864,25 @@ void main() { expect(statefulWidgetKeyB.currentState?.counter, equals(1)); }); }); + + ///Regression tests for https://github.com/flutter/flutter/issues/132557 + group('overridePlatformDefaultLocation', (){ + test('No initial location provided', () { + expect( () => GoRouter( + overridePlatformDefaultLocation: true, + routes: [ + GoRoute( + path: '/a', + builder: (BuildContext context, GoRouterState state) => const Placeholder(), + ), + GoRoute( + path: '/b', + builder: (BuildContext context, GoRouterState state) => const Placeholder(), + ), + ], + ), throwsA(const TypeMatcher())); + }); + }); } class TestInheritedNotifier extends InheritedNotifier> { From 0f1e823dbe4e676499fb95abd56e22c81e5d1478 Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Wed, 16 Aug 2023 00:53:22 +0530 Subject: [PATCH 03/18] Update CHANGELOG.md --- packages/go_router/CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 2eb1a7707c9..d325d20f4a5 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 10.1.1 + +- Optional parameter `overridePlatformDefaultLocation` to override initial route set/provided by platform. + ## 10.1.0 - Supports setting `requestFocus`. From 4519e8f3365e6fd4be9898a13d5468e21a7b4b5b Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Wed, 16 Aug 2023 18:47:54 +0530 Subject: [PATCH 04/18] Removed unused imports Rewritten test assert methods --- packages/go_router/lib/src/router.dart | 3 ++- packages/go_router/test/go_router_test.dart | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index f5d06e1e84b..5998f8e7277 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -93,7 +93,8 @@ class GoRouter implements RouterConfig { 'initialLocation must be set in order to use initialExtra', ), assert( - overridePlatformDefaultLocation ? initialLocation != null : true, + (overridePlatformDefaultLocation == true && initialLocation != null) + || (overridePlatformDefaultLocation == false), 'Initial location must be set to override platform default' ), assert( diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index 90b231b70e3..bb0c03cbcef 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -5,7 +5,6 @@ // ignore_for_file: cascade_invocations, diagnostic_describe_all_properties, unawaited_futures import 'dart:async'; -import 'dart:ui'; import 'package:collection/collection.dart'; import 'package:flutter/foundation.dart'; From eb3d6a273e6986e92bfeea8c7fbd21aacf4bacea Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Wed, 16 Aug 2023 18:56:18 +0530 Subject: [PATCH 05/18] Code reformat using `dart format` --- packages/go_router/lib/src/router.dart | 10 +++---- packages/go_router/test/go_router_test.dart | 32 ++++++++++++--------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 5998f8e7277..ea6959ffe60 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -93,10 +93,10 @@ class GoRouter implements RouterConfig { 'initialLocation must be set in order to use initialExtra', ), assert( - (overridePlatformDefaultLocation == true && initialLocation != null) - || (overridePlatformDefaultLocation == false), - 'Initial location must be set to override platform default' - ), + (overridePlatformDefaultLocation == true && + initialLocation != null) || + (overridePlatformDefaultLocation == false), + 'Initial location must be set to override platform default'), assert( (onException == null ? 0 : 1) + (errorPageBuilder == null ? 0 : 1) + @@ -522,7 +522,7 @@ class GoRouter implements RouterConfig { String _effectiveInitialLocation(String? initialLocation) { final String platformDefault = WidgetsBinding.instance.platformDispatcher.defaultRouteName; - if(overridePlatformDefaultLocation) { + if (overridePlatformDefaultLocation) { ///can force null check as it's already verified by ///asset() while initialization return initialLocation!; diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index bb0c03cbcef..77d174eb7e0 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -4865,21 +4865,25 @@ void main() { }); ///Regression tests for https://github.com/flutter/flutter/issues/132557 - group('overridePlatformDefaultLocation', (){ + group('overridePlatformDefaultLocation', () { test('No initial location provided', () { - expect( () => GoRouter( - overridePlatformDefaultLocation: true, - routes: [ - GoRoute( - path: '/a', - builder: (BuildContext context, GoRouterState state) => const Placeholder(), - ), - GoRoute( - path: '/b', - builder: (BuildContext context, GoRouterState state) => const Placeholder(), - ), - ], - ), throwsA(const TypeMatcher())); + expect( + () => GoRouter( + overridePlatformDefaultLocation: true, + routes: [ + GoRoute( + path: '/a', + builder: (BuildContext context, GoRouterState state) => + const Placeholder(), + ), + GoRoute( + path: '/b', + builder: (BuildContext context, GoRouterState state) => + const Placeholder(), + ), + ], + ), + throwsA(const TypeMatcher())); }); }); } From 4af6234a273bc77bb0ad27c350d513f7136e011d Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Sat, 19 Aug 2023 09:11:03 +0530 Subject: [PATCH 06/18] Update packages/go_router/CHANGELOG.md Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index d325d20f4a5..1e37f9f02a3 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,6 +1,6 @@ ## 10.1.1 -- Optional parameter `overridePlatformDefaultLocation` to override initial route set/provided by platform. +- Adds optional parameter `overridePlatformDefaultLocation` to override initial route set by platform. ## 10.1.0 From 253a96faa1e43cab10c1c63a693aa378c3aacf47 Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Sat, 19 Aug 2023 09:11:11 +0530 Subject: [PATCH 07/18] Update packages/go_router/lib/src/router.dart Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/lib/src/router.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index ea6959ffe60..bad8ba78961 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -93,9 +93,8 @@ class GoRouter implements RouterConfig { 'initialLocation must be set in order to use initialExtra', ), assert( - (overridePlatformDefaultLocation == true && - initialLocation != null) || - (overridePlatformDefaultLocation == false), + !overridePlatformDefaultLocation || initialLocation != null + , 'Initial location must be set to override platform default'), assert( (onException == null ? 0 : 1) + From 24e05dd9668f6bbd815608a3466c73819098cf29 Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Sat, 19 Aug 2023 09:22:37 +0530 Subject: [PATCH 08/18] Implement suggest documentation updates --- packages/go_router/lib/src/router.dart | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index ea6959ffe60..e9cbdbfbc2d 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -305,11 +305,13 @@ class GoRouter implements RouterConfig { @override late final GoRouteInformationParser routeInformationParser; - ///Defaults to [false], used to override platform default route mentioned - ///in MainActivity.kt. - ///If set to false, platform route will be used over `initialLocation` - ///set in `GoRouter` initialization. - ///See https://github.com/flutter/flutter/issues/132557 + /// When set to [true], the route returned by `getInitialRoute` will take precedence over the + /// platform's default initial location. This allows developers to control the starting route + /// of the application independently of the platform. + /// + /// Setting this parameter to [false] (default) will allow the platform's default initial + /// location to be used. It's advisable to only set this to true if you explicitly want to + /// ignore the platform's default initial location. final bool overridePlatformDefaultLocation; /// Returns `true` if there is at least two or more route can be pop. @@ -523,8 +525,8 @@ class GoRouter implements RouterConfig { final String platformDefault = WidgetsBinding.instance.platformDispatcher.defaultRouteName; if (overridePlatformDefaultLocation) { - ///can force null check as it's already verified by - ///asset() while initialization + // can force null check as it's already verified by + // asset() while initialization return initialLocation!; } if (initialLocation == null) { From 2491fbd0c026d208fce73aa150bff6f0ef61a63a Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Sat, 16 Sep 2023 19:39:57 +0530 Subject: [PATCH 09/18] add routing test --- packages/go_router/test/go_router_test.dart | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index d86c7a40fb6..f2c73bfc1f3 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -4932,6 +4932,29 @@ void main() { ), throwsA(const TypeMatcher())); }); + test('Test override using routeInformationProvider', () { + final String platformRoute = WidgetsBinding.instance.platformDispatcher.defaultRouteName; + const String expectedInitialRoute = '/abc'; + expect(platformRoute != expectedInitialRoute, isTrue); + + final GoRouter router = GoRouter( + overridePlatformDefaultLocation: true, + initialLocation: '/abc', + routes: [ + GoRoute( + path: '/abc', + builder: (BuildContext context, GoRouterState state) => + const Placeholder(), + ), + GoRoute( + path: '/bcd', + builder: (BuildContext context, GoRouterState state) => + const Placeholder(), + ), + ], + ); + expect(router.routeInformationProvider.value.uri.toString(), expectedInitialRoute); + }); }); } From d1c8ecebe7a5da8e2f5a18029257a9217e4a2f64 Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Sat, 16 Sep 2023 19:56:09 +0530 Subject: [PATCH 10/18] dart format + CHANGELOG fix --- packages/go_router/CHANGELOG.md | 1 - packages/go_router/lib/src/router.dart | 4 +--- packages/go_router/test/go_router_test.dart | 10 ++++++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 245234607fe..d15a0b2de39 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,4 +1,3 @@ -## NEXT ## 10.1.3 - Adds optional parameter `overridePlatformDefaultLocation` to override initial route set by platform. diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 502c9c78e54..37aceefecb9 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -92,9 +92,7 @@ class GoRouter implements RouterConfig { initialExtra == null || initialLocation != null, 'initialLocation must be set in order to use initialExtra', ), - assert( - !overridePlatformDefaultLocation || initialLocation != null - , + assert(!overridePlatformDefaultLocation || initialLocation != null, 'Initial location must be set to override platform default'), assert( (onException == null ? 0 : 1) + diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index f2c73bfc1f3..7bb43965c2d 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -4933,7 +4933,8 @@ void main() { throwsA(const TypeMatcher())); }); test('Test override using routeInformationProvider', () { - final String platformRoute = WidgetsBinding.instance.platformDispatcher.defaultRouteName; + final String platformRoute = + WidgetsBinding.instance.platformDispatcher.defaultRouteName; const String expectedInitialRoute = '/abc'; expect(platformRoute != expectedInitialRoute, isTrue); @@ -4944,16 +4945,17 @@ void main() { GoRoute( path: '/abc', builder: (BuildContext context, GoRouterState state) => - const Placeholder(), + const Placeholder(), ), GoRoute( path: '/bcd', builder: (BuildContext context, GoRouterState state) => - const Placeholder(), + const Placeholder(), ), ], ); - expect(router.routeInformationProvider.value.uri.toString(), expectedInitialRoute); + expect(router.routeInformationProvider.value.uri.toString(), + expectedInitialRoute); }); }); } From f05b37e215e0b74ec6ed5ada68a3f89472ab81e4 Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Fri, 22 Sep 2023 07:58:58 +0530 Subject: [PATCH 11/18] update docs and modify test --- packages/go_router/lib/src/router.dart | 20 +++++++---- packages/go_router/test/go_router_test.dart | 38 ++++++++++++--------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 95736286acd..b9cb4945635 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -302,13 +302,21 @@ class GoRouter implements RouterConfig { @override late final GoRouteInformationParser routeInformationParser; - /// When set to [true], the route returned by `getInitialRoute` will take precedence over the - /// platform's default initial location. This allows developers to control the starting route - /// of the application independently of the platform. + ///Whether to ignore platform's default initial location when + ///`initialLocation` is set. /// - /// Setting this parameter to [false] (default) will allow the platform's default initial - /// location to be used. It's advisable to only set this to true if you explicitly want to - /// ignore the platform's default initial location. + /// When set to [true], the route set as [initialLocation] will take + /// precedence over the platform's default initial location. + /// This allows developers to control the starting route of the application + /// independently of the platform. + /// + /// Platform's initial location is set when the app opens via a deeplink. + /// Use [overridePlatformDefaultLocation] only if one wants to override + /// platform implemented initial location. + /// + /// Setting this parameter to [false] (default) will allow the platform's + /// default initial location to be used even if the `initialLocation` is set. + /// It's advisable to only set this to [true] if one explicitly wants to. final bool overridePlatformDefaultLocation; /// Returns `true` if there is at least two or more route can be pop. diff --git a/packages/go_router/test/go_router_test.dart b/packages/go_router/test/go_router_test.dart index de773b18dcf..e486129b201 100644 --- a/packages/go_router/test/go_router_test.dart +++ b/packages/go_router/test/go_router_test.dart @@ -4977,27 +4977,33 @@ void main() { ), throwsA(const TypeMatcher())); }); - test('Test override using routeInformationProvider', () { + testWidgets('Test override using routeInformationProvider', + (WidgetTester tester) async { + tester.binding.platformDispatcher.defaultRouteNameTestValue = + '/some-route'; final String platformRoute = WidgetsBinding.instance.platformDispatcher.defaultRouteName; - const String expectedInitialRoute = '/abc'; + const String expectedInitialRoute = '/kyc'; expect(platformRoute != expectedInitialRoute, isTrue); - final GoRouter router = GoRouter( + final List routes = [ + GoRoute( + path: '/abc', + builder: (BuildContext context, GoRouterState state) => + const Placeholder(), + ), + GoRoute( + path: '/bcd', + builder: (BuildContext context, GoRouterState state) => + const Placeholder(), + ), + ]; + + final GoRouter router = await createRouter( + routes, + tester, overridePlatformDefaultLocation: true, - initialLocation: '/abc', - routes: [ - GoRoute( - path: '/abc', - builder: (BuildContext context, GoRouterState state) => - const Placeholder(), - ), - GoRoute( - path: '/bcd', - builder: (BuildContext context, GoRouterState state) => - const Placeholder(), - ), - ], + initialLocation: expectedInitialRoute, ); expect(router.routeInformationProvider.value.uri.toString(), expectedInitialRoute); From 67cbd8b786466560b28c43570b092313aeb8a4f9 Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Fri, 22 Sep 2023 07:59:10 +0530 Subject: [PATCH 12/18] add overridePlatformDefaultLocation to helpers --- packages/go_router/test/test_helpers.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/go_router/test/test_helpers.dart b/packages/go_router/test/test_helpers.dart index 38420cd2913..650fa13a2af 100644 --- a/packages/go_router/test/test_helpers.dart +++ b/packages/go_router/test/test_helpers.dart @@ -150,6 +150,7 @@ Future createRouter( String? restorationScopeId, GoExceptionHandler? onException, bool requestFocus = true, + bool overridePlatformDefaultLocation = false, }) async { final GoRouter goRouter = GoRouter( routes: routes, @@ -162,6 +163,7 @@ Future createRouter( navigatorKey: navigatorKey, restorationScopeId: restorationScopeId, requestFocus: requestFocus, + overridePlatformDefaultLocation: overridePlatformDefaultLocation, ); await tester.pumpWidget( MaterialApp.router( From 49a495a04fae61d6cce8246216ffe64d66be5ebd Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Fri, 22 Sep 2023 22:11:04 +0530 Subject: [PATCH 13/18] fix typo in comments --- packages/go_router/lib/src/router.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index b9cb4945635..47aae5bcea4 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -531,7 +531,7 @@ class GoRouter implements RouterConfig { WidgetsBinding.instance.platformDispatcher.defaultRouteName; if (overridePlatformDefaultLocation) { // can force null check as it's already verified by - // asset() while initialization + // assert() while initialization return initialLocation!; } if (initialLocation == null) { From b7e5dacec7eeb2c74ff3fca904c10f9a37b1c801 Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Fri, 22 Sep 2023 22:18:29 +0530 Subject: [PATCH 14/18] restructure to improve memory usage `platformDefault` is only assigned memory if `overridePlatformDefaultLocation` is false. Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/lib/src/router.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 47aae5bcea4..475724368b9 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -527,13 +527,13 @@ class GoRouter implements RouterConfig { } String _effectiveInitialLocation(String? initialLocation) { - final String platformDefault = - WidgetsBinding.instance.platformDispatcher.defaultRouteName; if (overridePlatformDefaultLocation) { // can force null check as it's already verified by // assert() while initialization return initialLocation!; } + final String platformDefault = + WidgetsBinding.instance.platformDispatcher.defaultRouteName; if (initialLocation == null) { return platformDefault; } else if (platformDefault == '/') { From 490e3da63972d5084c1b8adc73c1827878636506 Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:15:48 +0530 Subject: [PATCH 15/18] docs++ Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/lib/src/router.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 475724368b9..522aa3a18ea 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -302,8 +302,8 @@ class GoRouter implements RouterConfig { @override late final GoRouteInformationParser routeInformationParser; - ///Whether to ignore platform's default initial location when - ///`initialLocation` is set. + /// Whether to ignore platform's default initial location when + /// `initialLocation` is set. /// /// When set to [true], the route set as [initialLocation] will take /// precedence over the platform's default initial location. From 1fc46e3c919f9aa474765d9d245aa5809b60bcbf Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:15:57 +0530 Subject: [PATCH 16/18] ++ Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/lib/src/router.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 522aa3a18ea..764fe5b26b5 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -305,7 +305,7 @@ class GoRouter implements RouterConfig { /// Whether to ignore platform's default initial location when /// `initialLocation` is set. /// - /// When set to [true], the route set as [initialLocation] will take + /// When set to [true], the [initialLocation] will take /// precedence over the platform's default initial location. /// This allows developers to control the starting route of the application /// independently of the platform. From c37fbd7c592dbdfd84dd9ce892c01bf57652bbbc Mon Sep 17 00:00:00 2001 From: Delwin Mathew <84124091+opxdelwin@users.noreply.github.com> Date: Fri, 22 Sep 2023 23:16:09 +0530 Subject: [PATCH 17/18] ++ Co-authored-by: chunhtai <47866232+chunhtai@users.noreply.github.com> --- packages/go_router/lib/src/router.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 764fe5b26b5..039f414a476 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -528,8 +528,8 @@ class GoRouter implements RouterConfig { String _effectiveInitialLocation(String? initialLocation) { if (overridePlatformDefaultLocation) { - // can force null check as it's already verified by - // assert() while initialization + // The initialLocation must not be null as it's already + // verified by assert() during the initialization. return initialLocation!; } final String platformDefault = From 9aca1d648ec6c06563907004bd1fd23b0ee672e6 Mon Sep 17 00:00:00 2001 From: Delwin Mathew Date: Sat, 23 Sep 2023 01:09:37 +0530 Subject: [PATCH 18/18] dart format --- packages/go_router/lib/src/router.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/go_router/lib/src/router.dart b/packages/go_router/lib/src/router.dart index 039f414a476..534581abef3 100644 --- a/packages/go_router/lib/src/router.dart +++ b/packages/go_router/lib/src/router.dart @@ -528,7 +528,7 @@ class GoRouter implements RouterConfig { String _effectiveInitialLocation(String? initialLocation) { if (overridePlatformDefaultLocation) { - // The initialLocation must not be null as it's already + // The initialLocation must not be null as it's already // verified by assert() during the initialization. return initialLocation!; }