Ниже приведены мои 2 класса:
Код: Выделить всё
@JsonSerializable()
class InventoryResult {
List results;
int number;
int offset;
int totalResults;
InventoryResult({required this.results, required this.number, required this.offset, required this.totalResults});
factory InventoryResult.fromJson(Map json) => _$InventoryResultFromJson(json);
Map toJson() => _$InventoryResultToJson(this);
}
@JsonSerializable()
class Item {
int id;
String productName;
Item(
{required this.id,
required this.productName});
factory Item.fromJson(Map json) => _$ItemFromJson(json);
Map toJson() => _$ItemToJson(this);
}
Код: Выделить всё
InventoryResult _$InventoryResultFromJson(Map json) =>
InventoryResult(
results: (json['results'] as List)
.map((e) => Item.fromJson(e as Map))
.toList(),
number: (json['number'] as num).toInt(),
offset: (json['offset'] as num).toInt(),
totalResults: (json['totalResults'] as num).toInt(),
);
Map _$InventoryResultToJson(InventoryResult instance) =>
{
'results': instance.results, //INCORRECT NESTED CLASS HANDLING HERE
'number': instance.number,
'offset': instance.offset,
'totalResults': instance.totalResults,
};
Подробнее здесь: https://stackoverflow.com/questions/790 ... s-properly
Мобильная версия