Open hurelhuyag opened 10 months ago
I am not sure if this counts as a new bug, or just part of this one, but when attempting to work around this issue, the annotation @JsonKey(fromJson: foo, defaultValue: null)
results in the generated code not having a null default either, so there is no quick work around for this issue that I can see.
Yes, I'm surprised this is not the default behavior.
We have some JSON
objects that we have to parse in which we receive date fields as millisecondsSinceEpoch
.
I can't to:
@JsonKey(fromJson: DateTime.fromMillisecondsSinceEpoch)
final DateTime? myDate;
Because DateTime.fromMillisecondsSinceEpoch
returns a DateTime
instead of a DateTime?
. Because of this, I have to use a "helper" function:
@JsonKey(fromJson: parseDateTimeFromMilliseconds)
final DateTime? myDate;
// ...
DateTime? parseDateTimeFromMilliseconds(int? milliseconds) {
if (milliseconds == null) {
return null;
} else {
return DateTime.fromMillisecondsSinceEpoch(millisecond);
}
}
This seems so unnecessary.
Let's consider this example.
Currently when I run
build_runner
. The generated code will look like this.I'm suggesting if the property is nullable, then the generator can set a null value for the type property.