I am trying to fetch the weather forecast for each day, but I'M unable to determine which index I should use to fetch the temperature.
To get current weather temperature, I'm doing this:
activity.list[0].main.temp.toInt()
home_bloc:
class HomeBloc extends Bloc<HomeEvent, HomeState> {
final GetWeatherService _getWeatherService;
HomeBloc(this._getWeatherService) : super(HomeLoadingState()) {
on<LoadApiEvent>((event, emit) async {
// TODO: implement event handler
emit(HomeLoadingState());
final activity = await _getWeatherService.getWeather();
var sunRise =
DateTime.fromMillisecondsSinceEpoch(activity.city.sunrise * 1000);
var sunSet =
DateTime.fromMillisecondsSinceEpoch(activity.city.sunset * 1000);
emit(HomeLoadedState(
activity.city.name.toString(),
activity.list[0].main.temp.toInt(),
activity.list[0].weather[0].main.toString(),
{sunRise.hour: sunRise.minute},
{sunSet.hour: sunSet.minute},
));
});
}
}
How would you approach to get the temperature for each day besides today. I would like to have a 5 day forecast NOT hourly.
Classes:
class GetWeatherDetails {
GetWeatherDetails({
required this.cod,
required this.message,
required this.cnt,
required this.list,
required this.city,
});
String cod;
int message;
int cnt;
List<ListElement> list;
City city;
factory GetWeatherDetails.fromJson(Map<String, dynamic> json) =>
GetWeatherDetails(
cod: json["cod"],
message: json["message"],
cnt: json["cnt"],
list: List<ListElement>.from(
json["list"].map((x) => ListElement.fromJson(x))),
city: City.fromJson(json["city"]),
);
Map<String, dynamic> toJson() => {
"cod": cod,
"message": message,
"cnt": cnt,
"list": List<dynamic>.from(list.map((x) => x.toJson())),
"city": city.toString(),
};
}
class City {
City({
required this.id,
required this.name,
required this.coord,
required this.country,
required this.population,
required this.timezone,
required this.sunrise,
required this.sunset,
});
int id;
String name;
Coord coord;
String country;
int population;
int timezone;
int sunrise;
int sunset;
factory City.fromJson(Map<String, dynamic> json) => City(
id: json["id"],
name: json["name"],
coord: Coord.fromJson(json["coord"]),
country: json["country"],
population: json["population"],
timezone: json["timezone"],
sunrise: json["sunrise"],
sunset: json["sunset"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"coord": coord.toJson(),
"country": country,
"population": population,
"timezone": timezone,
"sunrise": sunrise,
"sunset": sunset,
};
}
class Coord {
Coord({
required this.lat,
required this.lon,
});
double lat;
double lon;
factory Coord.fromJson(Map<String, dynamic> json) => Coord(
lat: json["lat"].toDouble(),
lon: json["lon"].toDouble(),
);
Map<String, dynamic> toJson() => {
"lat": lat,
"lon": lon,
};
}
class ListElement {
ListElement({
required this.dt,
required this.main,
required this.weather,
required this.sys,
required this.dtTxt,
});
int dt;
Main main;
List<Weather> weather;
Sys sys;
DateTime dtTxt;
factory ListElement.fromJson(Map<String, dynamic> json) => ListElement(
dt: json["dt"],
main: Main.fromJson(json["main"]),
weather:
List<Weather>.from(json["weather"].map((x) => Weather.fromJson(x))),
sys: Sys.fromJson(json["sys"]),
dtTxt: DateTime.parse(json["dt_txt"]),
);
Map<String, dynamic> toJson() => {
"dt": dt,
"main": main.toJson(),
"weather": List<dynamic>.from(weather.map((x) => x.toJson())),
"sys": sys.toJson(),
"dt_txt": dtTxt.toIso8601String(),
};
}
class Main {
Main({
required this.temp,
required this.feelsLike,
required this.tempMin,
required this.tempMax,
});
double temp;
double feelsLike;
double tempMin;
double tempMax;
factory Main.fromJson(Map<String, dynamic> json) => Main(
temp: json["temp"].toDouble(),
feelsLike: json["feels_like"].toDouble(),
tempMin: json["temp_min"].toDouble(),
tempMax: json["temp_max"].toDouble(),
);
Map<String, dynamic> toJson() => {
"temp": temp,
"feels_like": feelsLike,
"temp_min": tempMin,
"temp_max": tempMax,
};
}
class Sys {
Sys({
required this.pod,
});
String pod;
factory Sys.fromJson(Map<String, dynamic> json) => Sys(
pod: json["pod"],
);
Map<String, dynamic> toJson() => {
"pod": pod,
};
}
class Weather {
Weather({
required this.id,
required this.main,
required this.description,
required this.icon,
});
int id;
String main;
String description;
String icon;
factory Weather.fromJson(Map<String, dynamic> json) => Weather(
id: json["id"],
main: json["main"],
description: json["description"],
icon: json["icon"],
);
Map<String, dynamic> toJson() => {
"id": id,
"main": main,
"description": description,
"icon": icon,
};
}
edited bloc_home (Throw the error, list element not of subtype int):
emit(HomeLoadedState(
activity.city.name.toString(),
{for (dynamic i in activity.list) {
print(activity.list[i].main.temp.toInt())
}},
activity.list[0].main.temp.toInt(),
activity.list[0].weather[0].main.toString(),
{sunRise.hour: sunRise.minute},
{sunSet.hour: sunSet.minute},
));
you need to loop through the 'list' array using for-loop and get every temp value. HomeLoadedState try this:
emit(HomeLoadedState(
activity.city.name.toString(),
//for(var i in activity.list){
//print(activity.list[i].main.temp.toInt()),
//here you are passing an int type, but it expecting List
//}
activity.list
activity.list[0].main.temp.toInt(),
activity.list[0].weather[0].main.toString(),
{sunRise.hour: sunRise.minute},
{sunSet.hour: sunSet.minute},
));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.