///我正在从API获取项目列表。但是,并非所有项目都有图像。我在仅获取“媒体”对象方面遇到了挑战;
// response
factory Item.fromJson(Map<String, dynamic> json) {
return new Posts(
name: json['package']['description'].toString(),
image: json['package']['image']['src'].toString(),
);
/// API响应
{
"code": 200,
"message": "OK",
"payload": {
"items": [
{
"package": {
"media": [
//可用媒体
{
"name": "2176AE9DA4142BD8-F79395896F3F0C698D01D504EA8644C9.jpeg",
"original_name": "a326702d-76da-4969-8369-baec0873d69a6987818660473624852.jpg",
"size": 1377517,
"src": "2176AE9DA4142BD8-F79395896F3F0C698D01D504EA8644C9.jpeg",
"mime_type": "image/jpeg",
"id": 228,
"created_at": "2019-03-19T10:23:25+00:00",
"updated_at": "2019-03-19T10:23:25+00:00"
}
]
},
"id": 227
},
//对象没有媒体
{
"id": 225
},
}
}