我正在嘗試從 OpenWeather 獲取當前天氣資訊,并在檢查是否有資料回傳后在 Text Widget 中顯示結果。但是,該條件 (snapshot.hasData) 始終回傳為 false,并且呼叫 else 條件 (CircularProgressIndicator)。 這是 FutureBuilder。
class WeatherPage extends StatefulWidget {
@override
State<WeatherPage> createState() => _WeatherPageState();
}
class _WeatherPageState extends State<WeatherPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
future: getCurrentWeather(),
builder: (context, snapshot) {
if (snapshot.hasData) {
WeatherModelCurrent weather =
snapshot.data as WeatherModelCurrent;
return weatherBox(weather);
} else {
return const CircularProgressIndicator();
}
},
),
),
);
}
這是 getCurrentWeather() 函式
Future getCurrentWeather() async {
WeatherModelCurrent? weather;
var url = Uri.parse(
"https://api.openweathermap.org/data/2.5/weather?q=Kathmandu&appid=82c7b99e2a8215351147f607592a3e63&units=metric");
var response = await http.get(url);
//response.body has the data returned by API call
print(response.body);
if (response.statusCode == 200) {
weather = WeatherModelCurrent.frommJson(jsonDecode(response.body));
} else {
print('error');
}
return weather;
}
這是模型類
class WeatherModelCurrent {
double temp;
double feelslike;
double tempmin;
double tempmax;
String description;
WeatherModelCurrent(
{required this.temp,
required this.feelslike,
required this.tempmin,
required this.tempmax,
required this.description});
factory WeatherModelCurrent.frommJson(Map<String, dynamic> jsonn) {
return WeatherModelCurrent(
temp: jsonn['main']['temp'].toDouble(),
feelslike: jsonn['main']['feels_like'].toDouble(),
tempmin: jsonn['main']['temp_min'].toDouble(),
tempmax: jsonn['main']['temp_max'].toDouble(),
description: jsonn['weather']['description'],
);
}
}
uj5u.com熱心網友回復:
Try & catch 你的getCurrentWeather代碼塊,我們可以得到以下例外:
flutter: exception type 'String' is not a subtype of type 'int' of 'index'
回應 json 是這樣的:
{
"weather":[
{
"id":803,
"main":"Clouds",
"description":"broken clouds",
"icon":"04d"
}
],
}
你的代碼description: jsonn['weather']['description']應該是description: jsonn['weather'][0]['description'],
uj5u.com熱心網友回復:
發生錯誤是因為您在錯誤的類中決議了 response.body。試試這個模型類
class WeatherModelCurrent {
WeatherModelCurrent({
this.coord,
this.weather,
this.base,
this.main,
this.visibility,
this.wind,
this.clouds,
this.dt,
this.sys,
this.timezone,
this.id,
this.name,
this.cod,
});
final Coord? coord;
final List<Weather>? weather;
final String? base;
final Main? main;
final int? visibility;
final Wind? wind;
final Clouds? clouds;
final int? dt;
final Sys? sys;
final int? timezone;
final int? id;
final String? name;
final int? cod;
factory WeatherModelCurrent.fromJson(Map<String, dynamic> json) => WeatherModelCurrent(
coord: Coord.fromJson(json["coord"]),
weather: List<Weather>.from(json["weather"].map((x) => Weather.fromJson(x))),
base: json["base"],
main: Main.fromJson(json["main"]),
visibility: json["visibility"],
wind: Wind.fromJson(json["wind"]),
clouds: Clouds.fromJson(json["clouds"]),
dt: json["dt"],
sys: Sys.fromJson(json["sys"]),
timezone: json["timezone"],
id: json["id"],
name: json["name"],
cod: json["cod"],
);
Map<String, dynamic> toJson() => {
"coord": coord!.toJson(),
"weather": List<dynamic>.from(weather!.map((x) => x.toJson())),
"base": base,
"main": main!.toJson(),
"visibility": visibility,
"wind": wind!.toJson(),
"clouds": clouds!.toJson(),
"dt": dt,
"sys": sys!.toJson(),
"timezone": timezone,
"id": id,
"name": name,
"cod": cod,
};
}
class Clouds {
Clouds({
this.all,
});
final int? all;
factory Clouds.fromJson(Map<String, dynamic> json) => Clouds(
all: json["all"],
);
Map<String, dynamic> toJson() => {
"all": all,
};
}
class Coord {
Coord({
this.lon,
this.lat,
});
final double? lon;
final double? lat;
factory Coord.fromJson(Map<String, dynamic> json) => Coord(
lon: json["lon"].toDouble(),
lat: json["lat"].toDouble(),
);
Map<String, dynamic> toJson() => {
"lon": lon,
"lat": lat,
};
}
class Main {
Main({
this.temp,
this.feelsLike,
this.tempMin,
this.tempMax,
this.pressure,
this.humidity,
});
final double? temp;
final double? feelsLike;
final double? tempMin;
final double? tempMax;
final int? pressure;
final int? humidity;
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(),
pressure: json["pressure"],
humidity: json["humidity"],
);
Map<String, dynamic> toJson() => {
"temp": temp,
"feels_like": feelsLike,
"temp_min": tempMin,
"temp_max": tempMax,
"pressure": pressure,
"humidity": humidity,
};
}
class Sys {
Sys({
this.type,
this.id,
this.country,
this.sunrise,
this.sunset,
});
final int? type;
final int? id;
final String? country;
final int? sunrise;
final int? sunset;
factory Sys.fromJson(Map<String, dynamic> json) => Sys(
type: json["type"],
id: json["id"],
country: json["country"],
sunrise: json["sunrise"],
sunset: json["sunset"],
);
Map<String, dynamic> toJson() => {
"type": type,
"id": id,
"country": country,
"sunrise": sunrise,
"sunset": sunset,
};
}
class Weather {
Weather({
this.id,
this.main,
this.description,
this.icon,
});
final int? id;
final String? main;
final String? description;
final 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,
};
}
class Wind {
Wind({
this.speed,
this.deg,
});
final double? speed;
final int? deg;
factory Wind.fromJson(Map<String, dynamic> json) => Wind(
speed: json["speed"].toDouble(),
deg: json["deg"],
);
Map<String, dynamic> toJson() => {
"speed": speed,
"deg": deg,
};
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/497918.html
下一篇:E/flutter(6339):[錯誤:flutter/lib/ui/ui_dart_state.cc(198)]未處理的例外:型別'TextEditingController'不
