You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class Source {
final String? url;
final List<String> works;
Source({
required this.url,
required this.works,
});
Map<String, dynamic> toJson() => {
if (url != null) 'url': url,
if (works.isNotEmpty) 'works': works,
};
I want to transition from my own handwritten toJson(), to a generated one.
for the url field i can use the includeIfNull: false option, but for the 2nd field, is there a similar option to omit lists if they are empty? i see no such option?
Likewise, is it possible to tolerate such a field not being set when decoding json, and setting it to [] on the object?
The text was updated successfully, but these errors were encountered:
What you propose would work for fields of type List<String>, but not List<String>? (although I think it's still a good idea for non-nullable lists, to save on serialization time and space).
This could be extended to other "empty types" such as the empty string (for String-typed fields that are not nullable), and even the integer value 0 (for numerical fields that are not nullable).
I have a class like so (simplified/redacted):
I want to transition from my own handwritten toJson(), to a generated one.
for the url field i can use the
includeIfNull: false
option, but for the 2nd field, is there a similar option to omit lists if they are empty? i see no such option?Likewise, is it possible to tolerate such a field not being set when decoding json, and setting it to [] on the object?
The text was updated successfully, but these errors were encountered: