I have a bizarre issue. I am trying to generate models with a freezed package. I have nested two classes that connected each other with different key values.
This is the main class that I will use. Parameters come from data key.
@freezed
class CustomClass with _$CustomClass {
@JsonSerializable(explicitToJson: true)
const factory CustomClass({
@JsonKey(name: "data.item1") required final String item1,
@JsonKey(name: "data.item2") required final CustomClass1 item2,
}) = _CustomClass;
factory CustomClass.fromJson(Map<String, dynamic> json) => _$CustomClassFromJson(json);
}
This is the subclass that I will use to generate my nested model.
@freezed
class CustomClass1 with _$CustomClass1 {
const factory CustomClass1({
required String name,
required num price,
}) = _CustomClass1;
factory CustomClass1.fromJson(Map<String, dynamic> json) => _$CustomClass1FromJson(json);
}
When I tried to model the response object data.item2, it gives the error below.
I/flutter (25343): NoSuchMethodError: The method '[]' was called on null.
I/flutter (25343): Receiver: null
I/flutter (25343): Tried calling: []("name")
Can you guys have suggestions?
Note: My response body ->
{
"data": {
"item1": "abcd",
"item2": { "name": "object_name", price: 52.90 }
},
"error": {}
}