the json I receive from the api looks like this:
{
"USD": [
{
"name": "something",
"value": 123
},
{
"name": "something else",
"value": 1234
}
],
"EUR": [
... same here
]
}
and my dart model looks like:
@JsonSerializable(anyMap: true, includeIfNull: true)
class StatisticsDto {
StatisticsDto({this.statistics});
final Map<String, List<StatisticsDetailDto>> statistics;
factory StatisticsDto.fromJson(Map<String, dynamic> json) =>
_$StatisticsDtoFromJson(json);
Map<String, dynamic> toJson() => _$StatisticsDtoToJson(this);
}
@JsonSerializable()
class StatisticsDetailDto {
StatisticsDetailDto({
this.name,
this.value,
});
final String name;
final num value;
factory StatisticsDetailDto.fromJson(Map<String, dynamic> json) =>
_$StatisticsDetailDtoFromJson(json);
Map<String, dynamic> toJson() => _$StatisticsDetailDtoToJson(this);
}
I don't seem to be able to make it work, I get a 200 from the api, but the serialization returns null.
What am I doing wrong here?
This is what your
JSON
objects should look like converted to Dart data class withjson_serializable
Dart data class with
json_serializable
withoutMap
support:Run:
In your first example, your Map statistics have no key that why it always returns null. JSON file would look like this for your first class Data