google / google/json_serializable.dart

Generate null checking code if the property is nullable when using `@JsonKey(fromJson)`

Open
#1,356 2 comments 2 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
1.6k
Forks
461
Avg merge
45m
Merged PRs (30d)
1

Description

Let's consider this example.
```
@JsonSerializable()
class Product {

@JsonKey(fromJson: ProductType.byId)
final ProductType? type;

const Product({required this.type});

}
```

Currently when I run `build_runner`. The generated code will look like this.
```
Product _$PFromJson(Map json) => Product(
type: ProductType.byId(json['type'] as String),
);
```

I'm suggesting if the property is nullable, then the generator can set a null value for the type property.
```
Product _$PFromJson(Map json) => Product(
type: json['type'] == null ? null : ProductType.byId(json['type'] as String),
);
```

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.