【发布时间】: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]
我不知道问题出在哪里..为什么我的输出会这样迭代?我只需要遍历每个项目并每次打印出每个项目,请帮忙!
【问题讨论】: