Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add set
  • Loading branch information
hannah-hyj committed Jan 8, 2025
commit c2845650e8a0d0ddbd678dab3b4ea4dc0aa70aa4
4 changes: 4 additions & 0 deletions packages/go_router_builder/lib/src/type_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ class _TypeHelperIterable extends _TypeHelper {
} else if (const TypeChecker.fromRuntime(Set)
.isAssignableFromType(parameterElement.type)) {
iterableCaster = '.toSet()';
if (!parameterElement.type.isNullableType &&
!parameterElement.hasDefaultValue) {
fallBack = '?? const {}';
}
}

return '''
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router_builder/test_inputs/list.dart.expect
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ extension $ListRouteExtension on ListRoute {
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);
}
5 changes: 5 additions & 0 deletions packages/go_router_builder/tool/run_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Future<void> main() async {
.where((File f) => f.path.endsWith('.dart'))
.toList();
for (final File file in testFiles) {
if(!file.path.contains('set.dart')) {
continue;
}
final String fileName = file.path.split('/').last;
final File expectFile = File(p.join('${file.path}.expect'));
if (!expectFile.existsSync()) {
Expand Down Expand Up @@ -59,6 +62,8 @@ Future<void> main() async {
.format(results.join('\n\n'))
.trim()
.replaceAll('\r\n', '\n');

print(generated);
expect(generated, equals(expectResult.replaceAll('\r\n', '\n')));
}, timeout: const Timeout(Duration(seconds: 100)));
}
Expand Down