[英]FLUTTER: How can I show the weather Icon from Open Weather Map?
[英]Flutter - Open weather map: Got current weather. How can I get forecast for next days?
我正在嘗試獲取每天的天氣預報,但我無法確定應該使用哪個索引來獲取溫度。
為了獲得當前的天氣溫度,我這樣做:
activity.list[0].main.temp.toInt()
家庭塊:
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},
));
});
}
}
除了今天,您將如何獲取每天的溫度。 我想要一個 5 天的預測而不是每小時。
課程:
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,
};
}
編輯 bloc_home (拋出錯誤,列表元素不是 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},
));
您需要使用 for 循環遍歷“列表”數組並獲取每個臨時值。 HomeLoadedState 試試這個:
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},
));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.