【问题标题】:_TypeError (type 'String' is not a subtype of type 'int' of 'index')_TypeError(类型'String'不是'index'类型'int'的子类型)
【发布时间】:2021-03-29 14:12:48
【问题描述】:

我有如下的json

{
    "title": "DVG's Critique of the Congress High",
    "body": [],
    "field_body": {
        "und": [
            {
                "value": "<p>The swift emergence of the High Command culture, which spawned the aforementioned dwarfs at many key-points revealed perhaps its most ugly face in the form of perpetually warring factions both in the Government and Party. 
            }
        ]
    },
}

Json 有两个 body 字段。如果 body 为 null 或 0,我应该从 field_body 加载数据,但是当我在 getData 中声明它给我错误时,“String”类型不是“index”类型“int”的子类型。谁能帮我解决这个问题。

    var data = await http.get(url);
    var jsonData = json.decode(utf8.decode(data.bodyBytes));
    return DetailData(
        jsonData['title'],
        jsonData['body']['und'][0]['value'] == 0
            ? jsonData['field_body']['und'][0]['value']
            : jsonData['body']['und'][0]['value'],
        jsonData['field_image_wp']['und'][0]['filename'],
        jsonData['name']);
  }

【问题讨论】:

    标签: flutter


    【解决方案1】:

    我猜你的 DetailData 类的第二个位置参数被键入为 int,你试图给它一个字符串值

    【讨论】:

      【解决方案2】:

      Json 键 "body" 没有任何 "und" 属性。它只是一个空列表。要么您在第 385 行错误地将 field_body 写为 body,要么您忘记在 Json 中将 und 属性添加到 body

      【讨论】:

        猜你喜欢
        • 2023-02-10
        • 2020-08-04
        • 2021-02-05
        • 2019-04-24
        • 1970-01-01
        • 2020-08-09
        • 1970-01-01
        • 2022-10-13
        • 1970-01-01
        相关资源
        最近更新 更多