google / google/json_serializable.dart

support decode null value in enum

Open
#1,245 1 comment 2 reactions 1 assignee Claimed by @kevmoo View on GitHub
Area: enums pkg:json_annotation pkg:json_serializable
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

v 4.7.0
for decode enum using next function
```
K $enumDecode(
Map enumValues,
Object? source, {
K? unknownValue,
})
```
with
```
if (source == null) {
throw ArgumentError(
'A value must be provided. Supported values: '
'${enumValues.values.join(', ')}',
);
}
```
so if im have next `enumValues` like
```
const _$FeedbackTypeEnumMap = {
FeedbackType.all: null,
FeedbackType.delivery: 'DELIVERY',
FeedbackType.payment: 'PAYMENT',
};
```
I cannot decode null value as `FeedbackType.all` because `if (source == null) throw `
and I have next exception:
> Invalid argument(s): A value must be provided. Supported values: null, DELIVERY, PAYMENT

for what we use the `throw`? if next steps is
```
for (var entry in enumValues.entries)
```
and then
```
if (unknownValue == null) {
throw ArgumentError(
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.