-
-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@Default using static const and fromJson
will generate code that does not compile
#82
Comments
|
Actually it doesn't sound to be related to if It sounds like |
This is generated automatically and if _$_StaticConstDefault _$_$_StaticConstDefaultFromJson(
Map<String, dynamic> json) {
return _$_StaticConstDefault(
json['value'] == null ? null : Duration(microseconds: json['value'] as int),
);
}
Map<String, dynamic> _$_$_StaticConstDefaultToJson(
_$_StaticConstDefault instance) =>
<String, dynamic>{
'value': instance.value?.inMicroseconds,
};
Okay, thanks 👍 |
In the mean time you could add an extra But this means deserializing the json will not add the default value |
Just to add my experience with the issue: List of enums as @default value will cause the same error, for example:
The enum:
the result:
|
Unfortunately I don't think there's anything I can do here. It's json_serializable that is rejecting the default value. But the |
Starting from freezed 2, this will work @freezed
class Cart with _$Cart {
const factory Cart({
@Default([]) List<int> items,
}) = _Cart;
factory Cart.fromJson(Map<String, dynamic> json) => _$CartFromJson(json);
}
@freezed
class User with _$User {
const factory User({
@Default(Cart()) Cart cart,
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
} Then Before freezed 2, use this workaround: @Default(const Cart()) @JsonKey(fromJson: _CartFromNullableJson) Cart cart, Cart _CartFromNullableJson(Map<String, dynamic>? json) =>
json == null ? Cart() : Cart.fromJson(json); |
I've tested this at 0.9.1 , supported by #75.
As tested here, this works well:
But, if
fromJson
line is added, it failed to compile.Error log:
The text was updated successfully, but these errors were encountered: