Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 4 additions & 0 deletions packages/go_router_builder/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.6

* Generates the const enum map for enums used in `List`, `Set` and `Iterable`.

## 1.1.5

* Replaces unnecessary Flutter SDK constraint with corresponding Dart
Expand Down
11 changes: 9 additions & 2 deletions packages/go_router_builder/lib/src/route_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,15 @@ GoRoute get $_routeGetterName => ${_routeDefinition()};
...routeDef._ctorParams,
...routeDef._ctorQueryParams,
]) {
if (ctorParam.type.isEnum) {
enumParamTypes.add(ctorParam.type as InterfaceType);
DartType potentialEnumType = ctorParam.type;
if (potentialEnumType is ParameterizedType &&
(ctorParam.type as ParameterizedType).typeArguments.isNotEmpty) {
potentialEnumType =
(ctorParam.type as ParameterizedType).typeArguments.first;
}

if (potentialEnumType.isEnum) {
enumParamTypes.add(potentialEnumType as InterfaceType);
}
}
}
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: 1.1.5
version: 1.1.6
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
1 change: 1 addition & 0 deletions packages/go_router_builder/test/builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ const Set<String> _expectedAnnotatedTests = <String>{
'EnumParam',
'DefaultValueRoute',
'NullableDefaultValueRoute',
'IterableWithEnumRoute'
};
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,57 @@ class NullableDefaultValueRoute extends GoRouteData {
NullableDefaultValueRoute({this.param = 0});
final int? param;
}

@ShouldGenerate(r'''
GoRoute get $iterableWithEnumRoute => GoRouteData.$route(
path: '/iterable-with-enum',
factory: $IterableWithEnumRouteExtension._fromState,
);

extension $IterableWithEnumRouteExtension on IterableWithEnumRoute {
static IterableWithEnumRoute _fromState(GoRouterState state) =>
IterableWithEnumRoute(
param: state.queryParametersAll['param']
?.map(_$EnumOnlyUsedInIterableEnumMap._$fromName),
);

String get location => GoRouteData.$location(
'/iterable-with-enum',
queryParams: {
if (param != null)
'param':
param?.map((e) => _$EnumOnlyUsedInIterableEnumMap[e]).toList(),
},
);

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

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

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

const _$EnumOnlyUsedInIterableEnumMap = {
EnumOnlyUsedInIterable.a: 'a',
EnumOnlyUsedInIterable.b: 'b',
EnumOnlyUsedInIterable.c: 'c',
};

extension<T extends Enum> on Map<T, String> {
T _$fromName(String value) =>
entries.singleWhere((element) => element.value == value).key;
}
''')
@TypedGoRoute<IterableWithEnumRoute>(path: '/iterable-with-enum')
class IterableWithEnumRoute extends GoRouteData {
IterableWithEnumRoute({this.param});

final Iterable<EnumOnlyUsedInIterable>? param;
}

enum EnumOnlyUsedInIterable {
a,
b,
c,
}