【问题标题】:How can I iterate over each item and render on the screen?如何遍历每个项目并在屏幕上呈现?
【发布时间】:2022-01-02 11:49:53
【问题描述】:

我一直在尝试迭代每个项目以获取预测数据,但无法确定输出的来源。我只是在尝试迭代温度、压力、地名、湿度、风速和感觉像..

json

{
  "cod": "200",
  "message": 0,
  "cnt": 40,
  "list": [
    {
      "dt": 1641124800,
      "main": {
        "temp": 11.74,
        "feels_like": 11.08,
        "temp_min": 11.74,
        "temp_max": 12,
        "pressure": 1012,
        "sea_level": 1012,
        "grnd_level": 1008,
        "humidity": 81,
        "temp_kf": -0.26
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 75
      },
      "wind": {
        "speed": 5.3,
        "deg": 229,
        "gust": 11.75
      },
      "visibility": 10000,
      "pop": 0,
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2022-01-02 12:00:00"
    },
    {
      "dt": 1641135600,
      "main": {
        "temp": 11.86,
        "feels_like": 11.11,
        "temp_min": 11.86,
        "temp_max": 12.09,
        "pressure": 1011,
        "sea_level": 1011,
        "grnd_level": 1006,
        "humidity": 77,
        "temp_kf": -0.23
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10d"
        }
      ],
      "clouds": {
        "all": 83
      },
      "wind": {
        "speed": 6.45,
        "deg": 223,
        "gust": 15.9
      },
      "visibility": 10000,
      "pop": 0.7,
      "rain": {
        "3h": 0.6
      },
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2022-01-02 15:00:00"
    },
    {
      "dt": 1641146400,
      "main": {
        "temp": 11.29,
        "feels_like": 10.43,
        "temp_min": 11.06,
        "temp_max": 11.29,
        "pressure": 1011,
        "sea_level": 1011,
        "grnd_level": 1007,
        "humidity": 75,
        "temp_kf": 0.23
      },
      "weather": [
        {
          "id": 500,
          "main": "Rain",
          "description": "light rain",
          "icon": "10n"
        }
      ],
      "clouds": {
        "all": 92
      },
      "wind": {
        "speed": 6.1,
        "deg": 250,
        "gust": 13.97
      },
      "visibility": 10000,
      "pop": 0.94,
      "rain": {
        "3h": 1.01
      },
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2022-01-02 18:00:00"
    },
    {
      "dt": 1641157200,
      "main": {
        "temp": 9.54,
        "feels_like": 6.37,
        "temp_min": 9.54,
        "temp_max": 9.54,
        "pressure": 1012,
        "sea_level": 1012,
        "grnd_level": 1009,
        "humidity": 76,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 802,
          "main": "Clouds",
          "description": "scattered clouds",
          "icon": "03n"
        }
      ],
      "clouds": {
        "all": 36
      },
      "wind": {
        "speed": 6.93,
        "deg": 244,
        "gust": 14.72
      },
      "visibility": 10000,
      "pop": 0,
      "sys": {
        "pod": "n"
      },
      "dt_txt": "2022-01-02 21:00:00"
    },
{
      "dt": 1641546000,
      "main": {
        "temp": 3.96,
        "feels_like": 0.41,
        "temp_min": 3.96,
        "temp_max": 3.96,
        "pressure": 1010,
        "sea_level": 1010,
        "grnd_level": 1007,
        "humidity": 82,
        "temp_kf": 0
      },
      "weather": [
        {
          "id": 804,
          "main": "Clouds",
          "description": "overcast clouds",
          "icon": "04d"
        }
      ],
      "clouds": {
        "all": 100
      },
      "wind": {
        "speed": 4.29,
        "deg": 239,
        "gust": 11.29
      },
      "visibility": 10000,
      "pop": 0.02,
      "sys": {
        "pod": "d"
      },
      "dt_txt": "2022-01-07 09:00:00"
    }
  ],
  "city": {
    "id": 2643743,
    "name": "London",
    "coord": {
      "lat": 51.5085,
      "lon": -0.1257
    },
    "country": "GB",
    "population": 1000000,
    "timezone": 0,
    "sunrise": 1641110761,
    "sunset": 1641139355
  }
}

在 forecast.dart 中

import 'package:forecast/models/weather_data.dart';

class ForecastData {
  final List list;

  ForecastData({required this.list});

  factory ForecastData.fromJson(Map<String, dynamic> json) {
    var list = json['list']?.map((e) => e)?.toList(growable: true) ?? [];
    List weatherData = [];
    list.forEach((e) {
      WeatherData w = WeatherData(
          placeName: json['city']['name'],
          temperature: e['main']['temp'],
          feels_like: e['main']["feels_like"],
          pressure: e['main']['pressure'],
          humidity: e['main']['humidity'],
          wind_speed: e['wind']['speed']);

      weatherData.add(w);
      print(weatherData.toList());
    });

    return ForecastData(list: weatherData);
  }
}

weatherdata.dart

class WeatherData {
  String? placeName;
  num? temperature;
  num? feels_like;
  num? pressure;
  num? humidity;
  num? wind_speed;

  WeatherData({
    this.placeName,
    this.temperature,
    this.feels_like,
    this.pressure,
    this.humidity,
    this.wind_speed,
  });
  @override
  String toString() =>
      '${placeName ?? 'unknown'}${feels_like ?? 'unknown'}${temperature ?? 'unknown'}${pressure ?? 'unknown'}${humidity ?? 'unknown'}${wind_speed ?? 'unknown'}';

  WeatherData.fromJson(Map<String, dynamic> json) {
    placeName = json['city']['name'];
    temperature = json['list'][0]['main']['temp'];
    feels_like = json['list'][0]['main']['temp'];
    pressure = json['list'][0]['main']['temp'];
    humidity = json['list'][0]['main']['temp'];
    wind_speed = json['list'][0]['main']['temp'];
  }
}

输出

I/flutter (25471): [London11.0811.741012815.3]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93, London6.229.481013757.18]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93, London6.229.481013757.18, London6.039.051013796.02]
I/flutter (25471): [London11.0811.741012815.3, London11.1111.861011776.45, London10.4311.291011756.1, London6.379.541012766.93, London6.229.481013757.18, London6.039.051013796.02, London5.698.681012795.66]

我不知道问题出在哪里..为什么我的输出会这样迭代?我只需要遍历每个项目并每次打印出每个项目,请帮忙!

【问题讨论】:

    标签: json flutter dart foreach


    【解决方案1】:

    forecast.dart 中,您在迭代器list.forEach 中打印整个weatherData 列表。

    尝试改变,

    print(weatherData.toList());
    

    到,

    print(w);
    

    并在您的toString 中添加一些空格

    ${placeName ?? 'unknown'} ${feels_like ?? 'unknown'} ${te...
    

    这样您就可以看到一个数字的结束位置和下一个数字的开始位置。

    【讨论】:

    • 是的,你是对的。我更改了代码以查看每个值,例如 print(w.temperature) ,似乎可以正常工作,但是当我运行print(w);时,这些值与我运行每个变量时不同,例如 print(w.temperature);also w 不包含 6 个值..但是输出如下: I/flutter (31970): London3.155.481015852.91 I/flutter (31970): London1.445.221015845.37 I/flutter (31970): London1.924.631016833.2 I/flutter (31970): London3.636.31016693 .69
    • 我只是想遍历温度、压力、地名、湿度、风速和感觉的每一项,并将它们添加到空列表weatherData并返回它,以便我可以渲染它屏幕..我该怎么做?
    【解决方案2】:

    我认为您应该丢失地图并查看 fromJson 参数...

      WeatherData.fromJson(Map<String, dynamic> json) {
        placeName = json['city']['name'];
        temperature = json['list'][0]['main']['temp'];
        feels_like = json['list'][0]['main']['temp'];
        pressure = json['list'][0]['main']['temp'];
        humidity = json['list'][0]['main']['temp'];
        wind_speed = json['list'][0]['main']['temp'];
      }

    【讨论】:

    • 我再次检查了它们并更改了 forecast.dart 中的代码 print(w.temperature) 以查看是否打印了正确的值并且确实如此。然后当我运行 print(w) 时,值不是我所期望的。输出如下: I/flutter (31970): London3.155.481015852.91 I/flutter (31970): London1.445.221015845.37 I/flutter ( 31970): London1.924.631016833.2 I/flutter (31970): London3.636.31016693.69
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2022-01-16
    • 1970-01-01
    • 2021-03-19
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多