Skip to content

Commit 03f83cf

Browse files
authored
[go_router_builder] Add a fallback for a not null List or Set param (flutter#8349)
fix issue: flutter#149675 ## Pre-launch Checklist - [ ] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] I read the [Tree Hygiene] page, which explains my responsibilities. - [ ] I read and followed the [relevant style guides] and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use `dart format`.) - [ ] I signed the [CLA]. - [ ] The title of the PR starts with the name of the package surrounded by square brackets, e.g. `[shared_preferences]` - [ ] I [linked to at least one issue that this PR fixes] in the description above. - [ ] I updated `pubspec.yaml` with an appropriate new version according to the [pub versioning philosophy], or this PR is [exempt from version changes]. - [ ] I updated `CHANGELOG.md` to add a description of the change, [following repository CHANGELOG style], or this PR is [exempt from CHANGELOG changes]. - [ ] I updated/added relevant documentation (doc comments with `///`). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [relevant style guides]: https://github.com/flutter/packages/blob/main/CONTRIBUTING.md#style [CLA]: https://cla.developers.google.com/ [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md [linked to at least one issue that this PR fixes]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [pub versioning philosophy]: https://dart.dev/tools/pub/versioning [exempt from version changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#version [following repository CHANGELOG style]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog-style [exempt from CHANGELOG changes]: https://github.com/flutter/flutter/blob/master/docs/ecosystem/contributing/README.md#changelog [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests
1 parent f7822a9 commit 03f83cf

File tree

7 files changed

+130
-2
lines changed

7 files changed

+130
-2
lines changed

packages/go_router_builder/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
## 2.7.3
3+
4+
- Fixes an issue when using a not null List or Set param.
5+
16
## 2.7.2
27

38
- Supports the latest `package:analyzer` and `package:source_gen`.

packages/go_router_builder/lib/src/type_helpers.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,18 +273,27 @@ class _TypeHelperIterable extends _TypeHelper {
273273

274274
// get correct type for iterable
275275
String iterableCaster = '';
276+
String fallBack = '';
276277
if (const TypeChecker.fromRuntime(List)
277278
.isAssignableFromType(parameterElement.type)) {
278279
iterableCaster = '.toList()';
280+
if (!parameterElement.type.isNullableType &&
281+
!parameterElement.hasDefaultValue) {
282+
fallBack = '?? const []';
283+
}
279284
} else if (const TypeChecker.fromRuntime(Set)
280285
.isAssignableFromType(parameterElement.type)) {
281286
iterableCaster = '.toSet()';
287+
if (!parameterElement.type.isNullableType &&
288+
!parameterElement.hasDefaultValue) {
289+
fallBack = '?? const {}';
290+
}
282291
}
283292

284293
return '''
285294
state.uri.queryParametersAll[
286295
${escapeDartString(parameterElement.name.kebab)}]
287-
?.map($entriesTypeDecoder)$iterableCaster''';
296+
?.map($entriesTypeDecoder)$iterableCaster$fallBack''';
288297
}
289298
return '''
290299
state.uri.queryParametersAll[${escapeDartString(parameterElement.name.kebab)}]''';

packages/go_router_builder/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: go_router_builder
22
description: >-
33
A builder that supports generated strongly-typed route helpers for
44
package:go_router
5-
version: 2.7.2
5+
version: 2.7.3
66
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
77
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22
88

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:go_router/go_router.dart';
6+
7+
@TypedGoRoute<ListRoute>(path: '/list-route')
8+
class ListRoute extends GoRouteData {
9+
ListRoute({
10+
required this.ids,
11+
this.nullableIds,
12+
this.idsWithDefaultValue = const <int>[0],
13+
});
14+
final List<int> ids;
15+
final List<int>? nullableIds;
16+
final List<int> idsWithDefaultValue;
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
RouteBase get $listRoute => GoRouteData.$route(
2+
path: '/list-route',
3+
factory: $ListRouteExtension._fromState,
4+
);
5+
6+
extension $ListRouteExtension on ListRoute {
7+
static ListRoute _fromState(GoRouterState state) => ListRoute(
8+
ids: state.uri.queryParametersAll['ids']?.map(int.parse).toList() ??
9+
const [],
10+
nullableIds: state.uri.queryParametersAll['nullable-ids']
11+
?.map(int.parse)
12+
.toList(),
13+
idsWithDefaultValue: state
14+
.uri.queryParametersAll['ids-with-default-value']
15+
?.map(int.parse)
16+
.toList() ??
17+
const <int>[0],
18+
);
19+
20+
String get location => GoRouteData.$location(
21+
'/list-route',
22+
queryParams: {
23+
'ids': ids.map((e) => e.toString()).toList(),
24+
if (nullableIds != null)
25+
'nullable-ids': nullableIds?.map((e) => e.toString()).toList(),
26+
if (idsWithDefaultValue != const <int>[0])
27+
'ids-with-default-value':
28+
idsWithDefaultValue.map((e) => e.toString()).toList(),
29+
},
30+
);
31+
32+
void go(BuildContext context) => context.go(location);
33+
34+
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
35+
36+
void pushReplacement(BuildContext context) =>
37+
context.pushReplacement(location);
38+
39+
void replace(BuildContext context) => context.replace(location);
40+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:go_router/go_router.dart';
6+
7+
@TypedGoRoute<SetRoute>(path: '/set-route')
8+
class SetRoute extends GoRouteData {
9+
SetRoute({
10+
required this.ids,
11+
this.nullableIds,
12+
this.idsWithDefaultValue = const <int>{0},
13+
});
14+
final Set<int> ids;
15+
final Set<int>? nullableIds;
16+
final Set<int> idsWithDefaultValue;
17+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
RouteBase get $setRoute => GoRouteData.$route(
2+
path: '/set-route',
3+
factory: $SetRouteExtension._fromState,
4+
);
5+
6+
extension $SetRouteExtension on SetRoute {
7+
static SetRoute _fromState(GoRouterState state) => SetRoute(
8+
ids: state.uri.queryParametersAll['ids']?.map(int.parse).toSet() ??
9+
const {},
10+
nullableIds: state.uri.queryParametersAll['nullable-ids']
11+
?.map(int.parse)
12+
.toSet(),
13+
idsWithDefaultValue: state
14+
.uri.queryParametersAll['ids-with-default-value']
15+
?.map(int.parse)
16+
.toSet() ??
17+
const <int>{0},
18+
);
19+
20+
String get location => GoRouteData.$location(
21+
'/set-route',
22+
queryParams: {
23+
'ids': ids.map((e) => e.toString()).toList(),
24+
if (nullableIds != null)
25+
'nullable-ids': nullableIds?.map((e) => e.toString()).toList(),
26+
if (idsWithDefaultValue != const <int>{0})
27+
'ids-with-default-value':
28+
idsWithDefaultValue.map((e) => e.toString()).toList(),
29+
},
30+
);
31+
32+
void go(BuildContext context) => context.go(location);
33+
34+
Future<T?> push<T>(BuildContext context) => context.push<T>(location);
35+
36+
void pushReplacement(BuildContext context) =>
37+
context.pushReplacement(location);
38+
39+
void replace(BuildContext context) => context.replace(location);
40+
}

0 commit comments

Comments
 (0)