【问题标题】:Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>'未处理的异常:“String”类型不是“List<dynamic>”类型的子类型
【发布时间】:2021-01-28 19:14:10
【问题描述】:

我正在尝试从 json 响应中解析来自 images 的图像,但我收到的错误说明为 Unhandled Exception: type 'String' is not a subtype of type 'List '

{
    "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'];
      });
    }
  }

我以正确的方式获取所有细节,除了图片

【问题讨论】:

  • 来自服务器的图像返回字符串而不是图像列表
  • images 应该是有效的 JSON 数组列表,而不是字符串。

标签: json flutter dart


【解决方案1】:

在您添加的 json 响应 sn-p 中,images 的值应该是一个数组,但它是双引号,基本上是一个字符串。

如果你把这个 sn-p 放到一个像 this 这样的 jsonValidator 中,它表明键 images 的值是一个字符串,而不是你期望的数组。

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"
    }
  ]
}

【讨论】:

    【解决方案2】:

    正如我在您的图片回复中看到的,您有这个 {'adsimg_img': 'some_url'}。

    所以我会在你的代码中这样做

    setState(() {
      .
      .
      .
      adId = jsonResponse['record']['adid'];
      postedById = jsonResponse['record']['aduserid'];
      images = (jsonResponse['record']['images'] as List)
        ?.map((i) => i['adsimg_img'])
        ?.toList();
    });
    

    所以最后的图像看起来像 ['url1', 'url2', ...] // 字符串列表

    我没有测试它,但它应该可以解决问题:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-26
      • 2021-05-21
      • 1970-01-01
      • 2021-01-16
      • 2021-08-15
      • 1970-01-01
      • 2019-08-21
      相关资源
      最近更新 更多