【问题标题】:How to put json-data into a list in flutter?如何将json-data放入flutter列表中?
【发布时间】:2020-07-28 12:05:59
【问题描述】:

当我尝试从这个 API https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=10&lon=10 获取数据时,它会得到一个包含所有时间序列的长数组。最后,我想显示每次下载的数组中都有自己位置的一些数据。我想将所有数据转换为一个列表,以便我可以操作数据,但是我得到了像这些类型'_InternalLinkedHashMap'这样的错误,它不是'String'类型的子类型。 这是我的代码

List<dynamic> timeseriesglobal = [];
  void loadForecast() async{

    //Getting the data from API
    Response response = await get("https://api.met.no/weatherapi/locationforecast/2.0/complete?lat=57.047218&lon=9.920100");

    var results = jsonDecode(response.body);

timeseriesglobal = results["properties"]["timeseries"] as List;


  }

最后我得到了显示数据的代码

child: ListView.builder(
                  itemCount: timeseriesglobal.length,
                  itemBuilder: (context,index){
                    return Card(
                      child: ListTile(
                        title: Text(
                          timeseriesglobal[index]
                        ),
                      ),
                    );
                  },


我做错了什么?请帮帮我

【问题讨论】:

    标签: api flutter dart weather


    【解决方案1】:

    提供您要显示 Ex time 的属性名称

    ListView.builder(
                      itemCount: timeseriesglobal.length,
                      itemBuilder: (context,index){
                        return Card(
                          child: ListTile(
                            title: Text(
                              timeseriesglobal[index]['time']
                            ),
                          ),
                        );
                      },
    

    【讨论】:

      【解决方案2】:

      从您从链接获得的 json 创建您的 BaseModel。 然后像下面这样解析

      var data= BaseModel.fromJson(response.body);

      现在这将包含所有内容,您可以从模型中提取您想要的任何内容 要转换 json,请使用 this link

      【讨论】:

        猜你喜欢
        • 2014-10-15
        • 1970-01-01
        • 2020-03-21
        • 2021-06-07
        • 2021-05-06
        • 2018-05-13
        • 2019-12-18
        • 1970-01-01
        • 2015-03-05
        相关资源
        最近更新 更多