[go_router] Fix a case-sensitive issue with name in version 4.0.0 or later (#2302)
* [go_router] Find a route by name in a case-insensitive
* [go_router] Test GoRouteInformationParser.namedLocation
* [go_router] Update pubspec and CHANGELOG
* [go_router] Add test pattern
* [go_router] Fix test
diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md
index 463f23d..dcaac3c 100644
--- a/packages/go_router/CHANGELOG.md
+++ b/packages/go_router/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 4.1.1
+
+- Fixes a bug where calling namedLocation does not support case-insensitive way.
+
## 4.1.0
- Adds `bool canPop()` to `GoRouterDelegate`, `GoRouter` and `GoRouterHelper`.
diff --git a/packages/go_router/lib/src/go_route_information_parser.dart b/packages/go_router/lib/src/go_route_information_parser.dart
index 4fc1149..a52fc6b 100644
--- a/packages/go_router/lib/src/go_route_information_parser.dart
+++ b/packages/go_router/lib/src/go_route_information_parser.dart
@@ -94,8 +94,9 @@
'${queryParams.isEmpty ? '' : ', queryParams: $queryParams'}');
return true;
}());
- assert(_nameToPath.containsKey(name), 'unknown route name: $name');
- final String path = _nameToPath[name]!;
+ final String keyName = name.toLowerCase();
+ assert(_nameToPath.containsKey(keyName), 'unknown route name: $name');
+ final String path = _nameToPath[keyName]!;
assert(() {
// Check that all required params are present.
final List<String> paramNames = <String>[];
diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml
index f122200..422a54c 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.1.0
+version: 4.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_route_information_parser_test.dart b/packages/go_router/test/go_route_information_parser_test.dart
index 067da09..6d33365 100644
--- a/packages/go_router/test/go_route_information_parser_test.dart
+++ b/packages/go_router/test/go_route_information_parser_test.dart
@@ -56,6 +56,44 @@
expect(matches[1].route, routes[0].routes[0]);
});
+ test('GoRouteInformationParser can retrieve route by name', () async {
+ final List<GoRoute> routes = <GoRoute>[
+ GoRoute(
+ path: '/',
+ builder: (_, __) => const Placeholder(),
+ routes: <GoRoute>[
+ GoRoute(
+ path: 'abc',
+ name: 'lowercase',
+ builder: (_, __) => const Placeholder(),
+ ),
+ GoRoute(
+ path: 'efg',
+ name: 'camelCase',
+ builder: (_, __) => const Placeholder(),
+ ),
+ GoRoute(
+ path: 'hij',
+ name: 'snake_case',
+ builder: (_, __) => const Placeholder(),
+ ),
+ ],
+ ),
+ ];
+ final GoRouteInformationParser parser = GoRouteInformationParser(
+ routes: routes,
+ redirectLimit: 100,
+ topRedirect: (_) => null,
+ );
+
+ expect(parser.namedLocation('lowercase'), '/abc?');
+ expect(parser.namedLocation('LOWERCASE'), '/abc?');
+ expect(parser.namedLocation('camelCase'), '/efg?');
+ expect(parser.namedLocation('camelcase'), '/efg?');
+ expect(parser.namedLocation('snake_case'), '/hij?');
+ expect(parser.namedLocation('SNAKE_CASE'), '/hij?');
+ });
+
test('GoRouteInformationParser returns error when unknown route', () async {
final List<GoRoute> routes = <GoRoute>[
GoRoute(