Skip to content

Commit ecbb67c

Browse files
authored
[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
1 parent 8079a2b commit ecbb67c

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.1.1
2+
3+
- Fixes a bug where calling namedLocation does not support case-insensitive way.
4+
15
## 4.1.0
26

37
- Adds `bool canPop()` to `GoRouterDelegate`, `GoRouter` and `GoRouterHelper`.

packages/go_router/lib/src/go_route_information_parser.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ class GoRouteInformationParser
9494
'${queryParams.isEmpty ? '' : ', queryParams: $queryParams'}');
9595
return true;
9696
}());
97-
assert(_nameToPath.containsKey(name), 'unknown route name: $name');
98-
final String path = _nameToPath[name]!;
97+
final String keyName = name.toLowerCase();
98+
assert(_nameToPath.containsKey(keyName), 'unknown route name: $name');
99+
final String path = _nameToPath[keyName]!;
99100
assert(() {
100101
// Check that all required params are present.
101102
final List<String> paramNames = <String>[];

packages/go_router/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 4.1.0
4+
version: 4.1.1
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/go_route_information_parser_test.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,44 @@ void main() {
5656
expect(matches[1].route, routes[0].routes[0]);
5757
});
5858

59+
test('GoRouteInformationParser can retrieve route by name', () async {
60+
final List<GoRoute> routes = <GoRoute>[
61+
GoRoute(
62+
path: '/',
63+
builder: (_, __) => const Placeholder(),
64+
routes: <GoRoute>[
65+
GoRoute(
66+
path: 'abc',
67+
name: 'lowercase',
68+
builder: (_, __) => const Placeholder(),
69+
),
70+
GoRoute(
71+
path: 'efg',
72+
name: 'camelCase',
73+
builder: (_, __) => const Placeholder(),
74+
),
75+
GoRoute(
76+
path: 'hij',
77+
name: 'snake_case',
78+
builder: (_, __) => const Placeholder(),
79+
),
80+
],
81+
),
82+
];
83+
final GoRouteInformationParser parser = GoRouteInformationParser(
84+
routes: routes,
85+
redirectLimit: 100,
86+
topRedirect: (_) => null,
87+
);
88+
89+
expect(parser.namedLocation('lowercase'), '/abc?');
90+
expect(parser.namedLocation('LOWERCASE'), '/abc?');
91+
expect(parser.namedLocation('camelCase'), '/efg?');
92+
expect(parser.namedLocation('camelcase'), '/efg?');
93+
expect(parser.namedLocation('snake_case'), '/hij?');
94+
expect(parser.namedLocation('SNAKE_CASE'), '/hij?');
95+
});
96+
5997
test('GoRouteInformationParser returns error when unknown route', () async {
6098
final List<GoRoute> routes = <GoRoute>[
6199
GoRoute(

0 commit comments

Comments
 (0)