Skip to content

Commit 58ac45e

Browse files
authored
[go_router_builder] Add support for Iterable, List and Set to TypedGoRoute (flutter#2679)
* fixes flutter#108437 support for iterable, list and set * fixes flutter#108437 url encoding for iterable, list and set * fixes flutter#108437 tests * format and analysis fix * fix string encoding * format * removed unused helper name * fix nullability checks for query params encoding * fix all_types.dart for new go router version * rebased to upstream * version * missing file regeneration
1 parent 0edae25 commit 58ac45e

File tree

8 files changed

+274
-15
lines changed

8 files changed

+274
-15
lines changed

packages/go_router_builder/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.2
2+
3+
* Adds support for Iterables, Lists and Sets in query params for TypedGoRoute. [#108437](https://github.com/flutter/flutter/issues/108437).
4+
15
## 1.1.1
26

37
* Support for the generation of the pushReplacement method has been added.

packages/go_router_builder/example/lib/all_types.dart

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ part 'all_types.g.dart';
2424
path: 'enhanced-enum-route/:requiredEnumField'),
2525
TypedGoRoute<StringRoute>(path: 'string-route/:requiredStringField'),
2626
TypedGoRoute<UriRoute>(path: 'uri-route/:requiredUriField'),
27+
TypedGoRoute<IterableRoute>(path: 'iterable-route'),
2728
])
2829
@immutable
2930
class AllTypesBaseRoute extends GoRouteData {
@@ -33,7 +34,6 @@ class AllTypesBaseRoute extends GoRouteData {
3334
Widget build(BuildContext context, GoRouterState state) =>
3435
const BasePage<void>(
3536
dataTitle: 'Root',
36-
param: null,
3737
);
3838
}
3939

@@ -290,17 +290,69 @@ class UriRoute extends GoRouteData {
290290
);
291291
}
292292

293+
class IterableRoute extends GoRouteData {
294+
IterableRoute({
295+
this.intIterableField,
296+
this.doubleIterableField,
297+
this.stringIterableField,
298+
this.boolIterableField,
299+
this.enumIterableField,
300+
this.intListField,
301+
this.doubleListField,
302+
this.stringListField,
303+
this.boolListField,
304+
this.enumListField,
305+
this.intSetField,
306+
this.doubleSetField,
307+
this.stringSetField,
308+
this.boolSetField,
309+
this.enumSetField,
310+
});
311+
312+
final Iterable<int>? intIterableField;
313+
final List<int>? intListField;
314+
final Set<int>? intSetField;
315+
316+
final Iterable<double>? doubleIterableField;
317+
final List<double>? doubleListField;
318+
final Set<double>? doubleSetField;
319+
320+
final Iterable<String>? stringIterableField;
321+
final List<String>? stringListField;
322+
final Set<String>? stringSetField;
323+
324+
final Iterable<bool>? boolIterableField;
325+
final List<bool>? boolListField;
326+
final Set<bool>? boolSetField;
327+
328+
final Iterable<SportDetails>? enumIterableField;
329+
final List<SportDetails>? enumListField;
330+
final Set<SportDetails>? enumSetField;
331+
332+
@override
333+
Widget build(BuildContext context, GoRouterState state) =>
334+
const BasePage<String>(
335+
dataTitle: 'IterableRoute',
336+
);
337+
338+
Widget drawerTile(BuildContext context) => ListTile(
339+
title: const Text('IterableRoute'),
340+
onTap: () => go(context),
341+
selected: GoRouter.of(context).location == location,
342+
);
343+
}
344+
293345
class BasePage<T> extends StatelessWidget {
294346
const BasePage({
295347
required this.dataTitle,
296-
required this.param,
348+
this.param,
297349
this.queryParam,
298350
this.queryParamWithDefaultValue,
299351
super.key,
300352
});
301353

302354
final String dataTitle;
303-
final T param;
355+
final T? param;
304356
final T? queryParam;
305357
final T? queryParamWithDefaultValue;
306358

@@ -352,6 +404,32 @@ class BasePage<T> extends StatelessWidget {
352404
requiredUriField: Uri.parse('https://dart.dev'),
353405
uriField: Uri.parse('https://dart.dev'),
354406
).drawerTile(context),
407+
IterableRoute(
408+
intIterableField: <int>[1, 2, 3],
409+
doubleIterableField: <double>[.3, .4, .5],
410+
stringIterableField: <String>['quo usque tandem'],
411+
boolIterableField: <bool>[true, false, false],
412+
enumIterableField: <SportDetails>[
413+
SportDetails.football,
414+
SportDetails.hockey,
415+
],
416+
intListField: <int>[1, 2, 3],
417+
doubleListField: <double>[.3, .4, .5],
418+
stringListField: <String>['quo usque tandem'],
419+
boolListField: <bool>[true, false, false],
420+
enumListField: <SportDetails>[
421+
SportDetails.football,
422+
SportDetails.hockey,
423+
],
424+
intSetField: <int>{1, 2, 3},
425+
doubleSetField: <double>{.3, .4, .5},
426+
stringSetField: <String>{'quo usque tandem'},
427+
boolSetField: <bool>{true, false},
428+
enumSetField: <SportDetails>{
429+
SportDetails.football,
430+
SportDetails.hockey,
431+
},
432+
).drawerTile(context),
355433
],
356434
)),
357435
body: Center(

packages/go_router_builder/example/lib/all_types.g.dart

Lines changed: 104 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/go_router_builder/example/lib/main.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/go_router_builder/example/test/all_types_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,15 @@ void main() {
125125
expect(find.text('UriRoute'), findsOneWidget);
126126
expect(find.text('Param: https://dart.dev'), findsOneWidget);
127127
expect(find.text('Query param: https://dart.dev'), findsOneWidget);
128+
129+
IterableRoute(
130+
intListField: <int>[1, 2, 3],
131+
).go(scaffoldState.context);
132+
await tester.pumpAndSettle();
133+
expect(find.text('IterableRoute'), findsOneWidget);
134+
expect(
135+
find.text(
136+
'/iterable-route?int-list-field=1&int-list-field=2&int-list-field=3'),
137+
findsOneWidget);
128138
});
129139
}

packages/go_router_builder/lib/src/route_config.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,10 @@ GoRoute get $_routeGetterName => ${_routeDefinition()};
215215
String get _locationArgs {
216216
final Iterable<String> pathItems = _parsedPath.map((Token e) {
217217
if (e is ParameterToken) {
218-
return '\${Uri.encodeComponent(${_encodeFor(e.name)})}';
218+
// Enum types are encoded using a map, so we need a nullability check
219+
// here to ensure it matches Uri.encodeComponent nullability
220+
final DartType? type = _field(e.name)?.returnType;
221+
return '\${Uri.encodeComponent(${_encodeFor(e.name)}${type?.isEnum ?? false ? '!' : ''})}';
219222
}
220223
if (e is PathToken) {
221224
return e.value;

0 commit comments

Comments
 (0)