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
Support using non-nullable JsonConverter on nullable properties #1136
Support using non-nullable JsonConverter on nullable properties #1136
Conversation
json_serializable/lib/src/type_helpers/json_converter_helper.dart
Outdated
Show resolved
Hide resolved
aa5e1ca
to
fe7d10f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gah! Just thought of this. Create just one function. Call it $convertOrNull
. There is zero benefit to have two identical functions with swapped generic arguments – right?
Good call, I didn't realise |
Actually, I'm not sure if that's a meaningful improvement Only one of the two functions (fromJson) is doing a cast, so their body has a difference. We always do the cast, but that'd negatively impact toJson which doesn't need one. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update the version to 6.3.0-dev and add a changelog entry
We can see if we can land you other change here, too!
Done |
Hi, I have problem with this merge. "Found more than one matching converter for I don't want to add annotation top of nullable field every time. Non-Nullable Converter
Nullable Converter
Example usage
|
You don't need to use both snnotations at the same time anymore. remove RefNulableResultConverter and your code should work |
Thank you, Actually, I tried that way, but there is a problem. PersonRef value should be null, but Ref.empty() value. |
It's just a thought but: is changing the |
I don't think it will fix it, Ref.empty() value will be assigned because it will still be null. There should be a null check and if the data is null the converter should not be called at all. |
I have this kind of issue too. Some of my classes have both DateTime and DateTime? attributes. And because my server returned a non-standard millisecond format, I had to create a JsonConverter class for it. Previously, approx. 6 months ago, my code generator worked fine when running build_runner using both the nullable and non-nullable versions of JsonConverter for DateTime, but now it returns an error "Found more than one matching converter for DateTime?." |
This allows using
JsonConverter<int, ...>
onint? value
.This also gracefully handles
null
JSON when usingJsonConverter<..., int>
onObject? value
, such thatnull
is returned instead of throwingfixes #822