Skip to content

Commit 32cd059

Browse files
committed
fix constant hooks
1 parent 7fc0b4f commit 32cd059

4 files changed

Lines changed: 16 additions & 4 deletions

File tree

packages/dart_mappable_builder/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Added support for `shallowEncoding` and `includeTypeId` options of `@MappableClass()`.
44
- Fixed escaping of enum values.
55
- Fixed copyWith method for unbounded and nullable-bounded generic types.
6+
- Fixed bug when using top-level variables as hooks.
67

78
# 4.4.0
89

packages/dart_mappable_builder/lib/src/elements/class/class_mapper_element.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:analyzer/dart/ast/ast.dart';
12
import 'package:analyzer/dart/element/element.dart';
23
import 'package:dart_mappable/dart_mappable.dart';
34

@@ -74,7 +75,11 @@ abstract class ClassMapperElement extends InterfaceMapperElement<ClassElement>
7475
if (hook != null && !hook.isNull) {
7576
var node = annotation.getPropertyNode('hook');
7677
if (node != null) {
77-
return node.toSource();
78+
var hook = node.toSource();
79+
if (node is InstanceCreationExpression) {
80+
hook = 'const $hook';
81+
}
82+
return hook;
7883
}
7984
}
8085
return null;

packages/dart_mappable_builder/lib/src/generators/mixins/decoding_mixin.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ mixin DecodingMixin on MapperGenerator<TargetClassMapperElement> {
1212
if (hook != null) {
1313
output.write('''
1414
@override
15-
final MappingHook hook = const $hook;
15+
final MappingHook hook = $hook;
1616
''');
1717
}
1818

1919
var superHooks = _getSuperHooks(element);
2020
if (superHooks.isNotEmpty) {
21+
final hook = superHooks.length == 1
22+
? superHooks.first
23+
: 'ChainedHook([${superHooks.map((h) => h.startsWith('const ') ? h.substring(6) : h).join(', ')}])';
2124
output.write('''
2225
@override
23-
final MappingHook superHook = const ${superHooks.length == 1 ? superHooks.first : 'ChainedHook([${superHooks.join(', ')}])'};
26+
final MappingHook superHook = $hook;
2427
2528
''');
2629
}

packages/dart_mappable_builder/lib/src/mapper_group.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,10 @@ class MapperElementGroup {
247247
resolveBounds: resolveBounds,
248248
resolveBoundsDeep: resolveBoundsDeep);
249249
}
250-
if (withNullability && t.isNullable && !type.endsWith('?')) {
250+
if (withNullability &&
251+
t.isNullable &&
252+
!type.endsWith('?') &&
253+
type != 'dynamic') {
251254
type += '?';
252255
}
253256
return type;

0 commit comments

Comments
 (0)