google / google/json_serializable.dart

Default value for record fields

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

Description

Since Version 6.7.0 Records are supported.

If I am right, there is currently no possibility to set a default value for a field inside the record.

For example:
```dart
@JsonSerializable()
class MyClass {
final ({
double numberWithDefault,
String someOtherText,
}) myRecord;

@JsonKey(defaultValue: 0.0)
final double anotherNumberWithDefault;

MyClass({
required this.myRecord,
required this.anotherNumberWithDefault,
});

factory MyClass.fromJson(Map json) => _$MyClassFromJson(json);
Map toJson() => _$MyClassToJson(this);
}
```

It would be nice to be able to set `numberWithDefault` to a default value, if the json value is null (just like `anotherNumberWithDefault`).

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.