Since my testJson is in a valid json format I do not think it is reasonable to expect the backend to change (which was the reasoning why issue #538 was closed).
If I change the autogenerated code and add an explicit cast
my result is a ParentObject where its ChildObject has value null
Are there any issues I don't recognize with the solutions I provide? Or is it possible to adapt the autogenerated code so that it can handle empty json objects
flutter --version
output: Flutter 3.10.5 • channel stable • https://github.com/flutter/flutter.git Framework • revision 796c8ef792 (5 months ago) • 2023-06-13 15:51:02 -0700 Engine • revision 45f6e00911 Tools • Dart 3.0.5 • DevTools 2.23.1My issue is basically the same as #538 , but I am not satisfied with the reasoning for closing the issue.
Suppose I have two classes, a parent class:
and a child class:
If I try to convert
via
ParentObject.fromJson(testJson);
into my class I get the following exception:because an empty
{}
is per default of type Map<dynamic, dynamic> and the cast in the autogenerated file subsequently throws an exceptionSince my
testJson
is in a valid json format I do not think it is reasonable to expect the backend to change (which was the reasoning why issue #538 was closed).If I change the autogenerated code and add an explicit cast
it seems to work and my result is a ParentObject with an ChildObject, where the 'testString' field in the ChildObject is null.
Additionally one could add a
treatEmptyAsNull
property as suggested in #538If I change the autogenerated code and add a check to see if the map is empty
my result is a ParentObject where its ChildObject has value
null
Are there any issues I don't recognize with the solutions I provide? Or is it possible to adapt the autogenerated code so that it can handle empty json objects
Thanks in advance