【问题标题】:Error on flutter : type 'List<dynamic>' is not a subtype of type 'String'颤振错误:类型“列表<动态>”不是“字符串”类型的子类型
【发布时间】:2021-08-22 05:35:40
【问题描述】:

我有这个 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] type 'List' 不是 type 'String' 的子类型"

【问题讨论】:

  • 展示你的模型
  • 您是否得到了发生错误的行?因为jsonArray[i]dynamic 类型,而您将其传递给fromJson 函数。您应该将什么传递给fromJsonList 还是需要 String
  • 我正在传递一个列表(产品)。我在 json 对象中有一个列表。

标签: json flutter dictionary


【解决方案1】:

您正在尝试将 String 类型的值分配给 product items 数组。您的响应之一可能是返回一个字符串,而您期望的是一个数组。请检查响应。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 如何打开这个设置窗口?
    • @CihanKalmaz 如果您使用的是 Visual Studio Code,您可以使用 (⌘ + ⇧ + D) 或从左侧菜单中打开运行和调试。
    猜你喜欢
    • 2021-08-09
    • 2021-05-16
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2022-11-22
    相关资源
    最近更新 更多