[英]Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>'
I'm trying to parse the images from images
from the json response, but I'm getting as error stating as Unhandled Exception: type 'String' is not a subtype of type 'List'我试图从解析图像images
从JSON响应,但我得到的错误,指出为未处理的异常:类型“字符串”不是类型的子类型“列表”
{
"status": true,
"record": {
"adid": "3",
"adsubcatid": "7",
"aduserid": "2",
"adname": "wfw fw",
"adcoverimg": "http://codecell.in/girrivi/assets/adcover/ic_service_search.png",
"addesc": "e fe fe ",
"ads_price": "5000",
"ads_location": "mysuru",
"created_date": "13/10/2020",
"user_name": "sunil",
"images": "{'adsimg_img':'http://codecell.in/girrivi/assets/adimgs/carpenter.png'},{'adsimg_img':'http://codecell.in/girrivi/assets/adimgs/electrician.png'},{'adsimg_img':'http://codecell.in/girrivi/assets/adimgs/gardner.png'}"
}
}
List images;
Future<String> getProductDetails() async {
String productDetailsUrl = Constant.productDetailsUrl+widget.id;
print(productDetailsUrl);
Map<String, String> headers = {'Content-Type' : 'application/json'};
final response = await http.get(productDetailsUrl, headers: headers);
print(response.body);
if(response.statusCode == 200){
jsonResponse = json.decode(response.body);
print('Response in Profile Screen: ' + response.body);
print("Price - "+jsonResponse['record']['ads_price']);
setState(() {
price = jsonResponse['record']['ads_price'];
title = jsonResponse['record']['adname'];
description = jsonResponse['record']['addesc'];
location = jsonResponse['record']['ads_location'];
publishedOn = jsonResponse['record']['created_date'];
postedBy = jsonResponse['record']['user_name'];
adId = jsonResponse['record']['adid'];
postedById = jsonResponse['record']['aduserid'];
images = jsonResponse['record']['images'];
});
}
}
I am getting all the details in the right way except the images除了图像,我以正确的方式获取所有细节
As I see in your images response you have this {'adsimg_img': 'some_url'}.正如我在您的图片回复中看到的那样,您有这个 {'adsimg_img': 'some_url'}。
So I would do like this in your code所以我会在你的代码中这样做
setState(() {
.
.
.
adId = jsonResponse['record']['adid'];
postedById = jsonResponse['record']['aduserid'];
images = (jsonResponse['record']['images'] as List)
?.map((i) => i['adsimg_img'])
?.toList();
});
So images at the end will look like ['url1', 'url2', ...] // List of Strings所以最后的图像看起来像 ['url1', 'url2', ...] // 字符串列表
I didn't tested it but it should do the trick :)我没有测试它,但它应该可以解决问题:)
In the json response snippet that you have added, the value of images
should be an array, but it is in double quotes which is a basically string.在您添加的 json 响应片段中, images
的值应该是一个数组,但它在双引号中,基本上是一个字符串。
If you put that snippet into a jsonValidator like this one, it shows that the value of the key images
is a string, rather than the array that you are expecting.如果将该片段放入像这样的 jsonValidator 中,它会显示关键images
的值是一个字符串,而不是您期望的数组。
An array of objects in json is formatted like in this one json 中的一组对象的格式与此类似
{
"user_name": "sunil",
"images": [
{
"adsimg_img":"http://codecell.in/girrivi/assets/adimgs/carpenter.png"
},
{
"adsimg_img":"http://codecell.in/girrivi/assets/adimgs/carpenter.png"
},
{
"adsimg_img":"http://codecell.in/girrivi/assets/adimgs/carpenter.png"
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.