Flutter 来自服务器响应的颤振返回数组
在我的代码的这一部分Flutter 来自服务器响应的颤振返回数组,flutter,Flutter,在我的代码的这一部分等待webApi.getKeywords()返回可以从服务器获取的数组,现在当我尝试返回时,我得到错误: type 'List<dynamic>' is not a subtype of type 'String' 返回响应。正文: [ { "id": 1, "user_id": 1, "title": "asdasdasd", "description": "asdasdasd", "type": "post",
等待webApi.getKeywords()代码>返回可以从服务器获取的数组,现在当我尝试返回时,我得到错误:
type 'List<dynamic>' is not a subtype of type 'String'
返回响应。正文
:
[
{
"id": 1,
"user_id": 1,
"title": "asdasdasd",
"description": "asdasdasd",
"type": "post",
"slug": "asdasdad",
"featured_images": {
"images": {
"300": "/uploads/post_images/2019/300_1573573784.png",
"600": "/uploads/post_images/2019/600_1573573784.png",
"900": "/uploads/post_images/2019/900_1573573784.png",
"original": "/uploads/post_images/2019/1573573784.png"
},
"thumbnail": "/uploads/post_images/2019/300_1573573784.png"
},
"lang": "fa",
"visit": 0,
"categories": [
{
"id": 1,
"title": "aaaaaaa",
"lang": "fa",
"parent": 0,
"pivot": {
"contents_id": 1,
"content_categories_id": 1
}
}
]
},
{
"id": 2,
"user_id": 1,
"title": "asdasdasd",
"description": "asdadasd",
"type": "post",
"slug": "asdasdasda",
"featured_images": {
"images": {
"300": "/uploads/post_images/2019/300_1573573846.png",
"600": "/uploads/post_images/2019/600_1573573846.png",
"900": "/uploads/post_images/2019/900_1573573846.png",
"original": "/uploads/post_images/2019/1573573846.png"
},
"thumbnail": "/uploads/post_images/2019/300_1573573846.png"
},
"lang": "fa",
"visit": 0,
"categories": [
{
"id": 2,
"title": "bbbbbbbb",
"lang": "fa",
"parent": 0,
"pivot": {
"contents_id": 2,
"content_categories_id": 2
}
}
]
}
]
问题出在这行代码上:
json.decode(response.body)
试试这个:
list = List<KeywordsResponse>.from(response.body.map((x) => KeywordsResponse.fromJson(x)));
list=list.from(response.body.map((x)=>KeywordsResponse.fromJson(x));
json.decode
不需要,因为response.body
已经解码了的列表
maps@pskink我想将response.body
返回为List
formatso调用response.body
上的map()
方法(已对映射的List
进行json解码)@pskinkresponse.body.map((item)=>list.add(关键字response(item['id'],item['title'],item['description']))代码>不起作用response.body.map((item)=>KeywordsResponse.fromJson(item)).toList()
甚至更好:response.body.map(关键字response.fromJson).toList()代码>但我不能100%确定它是否有效
json.decode(response.body)
list = List<KeywordsResponse>.from(response.body.map((x) => KeywordsResponse.fromJson(x)));