Skip to content

Commit feadf4c

Browse files
authored
json_serializable: more enum util cleanup (#1205)
Use a field to avoid extra null checks and {} is String interpolation
1 parent 12414e0 commit feadf4c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

json_serializable/lib/src/enum_utils.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,30 @@ Object? _generateEntry({
5353
const TypeChecker.fromRuntime(JsonValue).firstAnnotationOfExact(field);
5454

5555
if (annotation == null) {
56-
if (jsonEnum.valueField != null) {
56+
final valueField = jsonEnum.valueField;
57+
if (valueField != null) {
5758
// TODO: fieldRename is pointless here!!! At least log a warning!
5859

5960
final fieldElementType = field.type.element2 as EnumElement;
6061

61-
final e = fieldElementType.getField(jsonEnum.valueField!);
62+
final e = fieldElementType.getField(valueField);
6263

6364
if (e == null || e.isStatic) {
6465
throw InvalidGenerationSourceError(
65-
'`JsonEnum.valueField` was set to "${jsonEnum.valueField}", but '
66+
'`JsonEnum.valueField` was set to "$valueField", but '
6667
'that is not a valid, instance field on '
6768
'`${typeToCode(targetType)}`.',
6869
element: targetType.element2,
6970
);
7071
}
7172

7273
final reader = ConstantReader(field.computeConstantValue());
73-
final valueReader = reader.read(jsonEnum.valueField!);
74+
final valueReader = reader.read(valueField);
7475
if (valueReader.validValueType) {
7576
return valueReader.literalValue;
7677
} else {
7778
throw InvalidGenerationSourceError(
78-
'`JsonEnum.valueField` was set to "${jsonEnum.valueField}", but '
79+
'`JsonEnum.valueField` was set to "$valueField", but '
7980
'that field does not have a type of String, int, or null.',
8081
element: targetType.element2,
8182
);

0 commit comments

Comments
 (0)