颤振错误:“List<dynamic>”类型不是“String”类型的子类型

时间:2021-06-04 06:43:50

标签: json flutter dictionary

我有这个 Json。我想映射我的 json 数据。但是我得到了一些错误。

{
"Id": 0,
"Product_Id": 0,
"Quantity": 0,
"User_Id": "a49a10d2-fc3f-477a-b087-5b0d07545964",
"Active": false,
"CartProducts": null,
"Products": [
    {
        "Id": 116,
        "Shop_Id": 1,
        "Offer": 0.0,
        "Quantity": 1,
        "Price": 100.0,
        "Category_Id": 0,
        "Description": null,
        "Name": "Lacoste Product",
        "Active": false,
        "Size": "small",
        "Color": "black",
        "Is_External_Product": true,
        "External_Link": "https://www.lacoste.com.tr/urun/kadin-kirmizi-polo-pf0504-007-4/",
        "Currency": null,
        "ProductImages": null
    }
]
} 

我在这里解码Json

if(jsonObject['Products']!=null){
  productItems = ProductItem.getListFromJson(jsonObject['Products']);
}

static List<ProductItem> getListFromJson(List<dynamic> jsonArray) {
log("getListFromJson");
List<ProductItem> list = [];
for (int i = 0; i < jsonArray.length; i++) {
  list.add(ProductItem.fromJson(jsonArray[i]));
}
return list;
 }

但我收到此错误。 “[log] 类型 'List' 不是类型 'String' 的子类型”

2 个答案:

答案 0 :(得分:1)

您正在尝试将字符串类型的值分配给产品项数组。您的其中一个响应可能返回一个字符串,而您需要一个数组。请检查回复。

答案 1 :(得分:0)

这是一个 JSON 解析问题,它可能与作为列表或字符串的“产品”没有直接关系。

因此,为了更好地诊断此错误或未来的其他错误,您可能需要启用 dart 调试器,同时检查“未捕获的异常”选项,这将引导您解决类型转换问题。

Enable dart debugger for uncaught exceptions