【问题标题】:accessing json elements in dart访问 dart 中的 json 元素
【发布时间】:2019-02-17 03:31:32
【问题描述】:

我正在尝试访问 dart 中的 json 元素

我测试了一下 json 格式是什么。最初是这样的:(只是一个示例)

{
    "trails": [
        {
        "id": 7002454,
        "name": "Bidwell Park Tour",
        "location": "Chico, California",
        "longitude": -121.7919,
        "latitude": 39.7617,
        }
}

在获取数据并解码后,我得到了 trails 对象中的字符串。然后我对其进行编码以将其恢复为 json,我得到了这种格式:

      [{
        "id": 7002454,
        "name": "Bidwell Park Tour",
        "location": "Chico, California",
        "longitude": -121.7919,
        "latitude": 39.7617,
        }]

这是我迄今为止尝试过的代码:

return ListView.builder(
        itemCount: trails.length,
        itemBuilder: (context, int index) {
       //string representation of the json returned, commented out to avoid 
       //errors in multiple returns
      //return Text( trails[index].trails.toString());
        //encoded back to json (not sure if I'm assigning it into the right 
        //variable type
        Object myText  = json.encode(trails[index].trails);
        List<Map> myText2 = json.decode(myText);

        return Text(myText2[0]['name']);

我目前拥有它的方式,它正在返回:

     List<dynamic> is not a subtype of type List<Map<dynamic, dynamic>>. 

我不确定需要改变什么。如果有人想查看我在哪里获取数据,我可以发布更多代码。感谢您的帮助。

【问题讨论】:

    标签: json dart flutter


    【解决方案1】:

    List&lt;Map&gt; 更改为List&lt;dynamic&gt;

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2023-03-10
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 2021-09-27
      • 2019-09-04
      相关资源
      最近更新 更多