简体   繁体   English

未处理的异常:“String”类型不是“List”类型的子类型<dynamic> &#39;

[英]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.

相关问题 未处理的异常:键入“列表”<dynamic> &#39; 不是 &#39;List 类型的子类型<String> &#39; - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String>' 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,> - Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> ' 在类型转换中?</dynamic></string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast? 未处理的异常:键入“列表”<dynamic> ' 不是类型 'List 的子类型<model> '</model></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Model>' 未处理的异常:“字符串”类型不是“地图”类型的子类型<string, dynamic> '</string,> - Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'String' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String' 未处理的异常:键入“列表”<dynamic> &#39; 不是类型 &#39;Map 的子类型<String, dynamic> &#39; 在颤动中如何像这样发布 json obj - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in flutter how to post json obj like this 未处理的异常:键入“列表<dynamic> ' 不是 'Map 类型的子类型<string, dynamic> ' flutter 错误</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' flutter error 从 api 错误中获取 Json 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> '</dynamic></string,> - Fetching Json from api error Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' 未处理的异常:键入“列表”<dynamic> ' 不是类型 'Map 的子类型<string, dynamic> ' 在飞镖/ flutter</string,></dynamic> - Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' in dart/ flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM