Skip to content

Commit

Permalink
[go_router_builder] Add a fallback for a not null List or Set param (#…
Browse files Browse the repository at this point in the history
…8349)

fix issue:  flutter/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
  • Loading branch information
hannah-hyj authored Jan 8, 2025
1 parent f7822a9 commit 03f83cf
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

## 2.7.3

- Fixes an issue when using a not null List or Set param.

## 2.7.2

- Supports the latest `package:analyzer` and `package:source_gen`.
Expand Down
11 changes: 10 additions & 1 deletion packages/go_router_builder/lib/src/type_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,18 +273,27 @@ class _TypeHelperIterable extends _TypeHelper {

// get correct type for iterable
String iterableCaster = '';
String fallBack = '';
if (const TypeChecker.fromRuntime(List)
.isAssignableFromType(parameterElement.type)) {
iterableCaster = '.toList()';
if (!parameterElement.type.isNullableType &&
!parameterElement.hasDefaultValue) {
fallBack = '?? const []';
}
} else if (const TypeChecker.fromRuntime(Set)
.isAssignableFromType(parameterElement.type)) {
iterableCaster = '.toSet()';
if (!parameterElement.type.isNullableType &&
!parameterElement.hasDefaultValue) {
fallBack = '?? const {}';
}
}

return '''
state.uri.queryParametersAll[
${escapeDartString(parameterElement.name.kebab)}]
?.map($entriesTypeDecoder)$iterableCaster''';
?.map($entriesTypeDecoder)$iterableCaster$fallBack''';
}
return '''
state.uri.queryParametersAll[${escapeDartString(parameterElement.name.kebab)}]''';
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router_builder/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: go_router_builder
description: >-
A builder that supports generated strongly-typed route helpers for
package:go_router
version: 2.7.2
version: 2.7.3
repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22

Expand Down
17 changes: 17 additions & 0 deletions packages/go_router_builder/test_inputs/list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:go_router/go_router.dart';

@TypedGoRoute<ListRoute>(path: '/list-route')
class ListRoute extends GoRouteData {
ListRoute({
required this.ids,
this.nullableIds,
this.idsWithDefaultValue = const <int>[0],
});
final List<int> ids;
final List<int>? nullableIds;
final List<int> idsWithDefaultValue;
}
40 changes: 40 additions & 0 deletions packages/go_router_builder/test_inputs/list.dart.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
RouteBase get $listRoute => GoRouteData.$route(
path: '/list-route',
factory: $ListRouteExtension._fromState,
);

extension $ListRouteExtension on ListRoute {
static ListRoute _fromState(GoRouterState state) => ListRoute(
ids: state.uri.queryParametersAll['ids']?.map(int.parse).toList() ??
const [],
nullableIds: state.uri.queryParametersAll['nullable-ids']
?.map(int.parse)
.toList(),
idsWithDefaultValue: state
.uri.queryParametersAll['ids-with-default-value']
?.map(int.parse)
.toList() ??
const <int>[0],
);

String get location => GoRouteData.$location(
'/list-route',
queryParams: {
'ids': ids.map((e) => e.toString()).toList(),
if (nullableIds != null)
'nullable-ids': nullableIds?.map((e) => e.toString()).toList(),
if (idsWithDefaultValue != const <int>[0])
'ids-with-default-value':
idsWithDefaultValue.map((e) => e.toString()).toList(),
},
);

void go(BuildContext context) => context.go(location);

Future<T?> push<T>(BuildContext context) => context.push<T>(location);

void pushReplacement(BuildContext context) =>
context.pushReplacement(location);

void replace(BuildContext context) => context.replace(location);
}
17 changes: 17 additions & 0 deletions packages/go_router_builder/test_inputs/set.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:go_router/go_router.dart';

@TypedGoRoute<SetRoute>(path: '/set-route')
class SetRoute extends GoRouteData {
SetRoute({
required this.ids,
this.nullableIds,
this.idsWithDefaultValue = const <int>{0},
});
final Set<int> ids;
final Set<int>? nullableIds;
final Set<int> idsWithDefaultValue;
}
40 changes: 40 additions & 0 deletions packages/go_router_builder/test_inputs/set.dart.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
RouteBase get $setRoute => GoRouteData.$route(
path: '/set-route',
factory: $SetRouteExtension._fromState,
);

extension $SetRouteExtension on SetRoute {
static SetRoute _fromState(GoRouterState state) => SetRoute(
ids: state.uri.queryParametersAll['ids']?.map(int.parse).toSet() ??
const {},
nullableIds: state.uri.queryParametersAll['nullable-ids']
?.map(int.parse)
.toSet(),
idsWithDefaultValue: state
.uri.queryParametersAll['ids-with-default-value']
?.map(int.parse)
.toSet() ??
const <int>{0},
);

String get location => GoRouteData.$location(
'/set-route',
queryParams: {
'ids': ids.map((e) => e.toString()).toList(),
if (nullableIds != null)
'nullable-ids': nullableIds?.map((e) => e.toString()).toList(),
if (idsWithDefaultValue != const <int>{0})
'ids-with-default-value':
idsWithDefaultValue.map((e) => e.toString()).toList(),
},
);

void go(BuildContext context) => context.go(location);

Future<T?> push<T>(BuildContext context) => context.push<T>(location);

void pushReplacement(BuildContext context) =>
context.pushReplacement(location);

void replace(BuildContext context) => context.replace(location);
}

0 comments on commit 03f83cf

Please sign in to comment.