[英]What is the error in my code and how do I parse the data from a json successfully?
我正在 Flutter 中开发天气应用程序并遇到一个问题,当我们在搜索栏中输入城市名称并单击下一个图标时,它会在屏幕上显示该特定地点的天气。 但是为了确保正确解析数据,我只在控制台中调用数据。 虽然代码没有显示错误,但控制台中会抛出多个错误。 谁能告诉我如何解决此错误并成功解析此数据?
控制台中显示的错误如下:
对不起,代码中的实际错误如下:
观察站调试器和分析器在Chrome,请访问: http://127.0.0.1:50548/vYt04KYoAA8=的颤振DevTools调试器和分析器在Chrome,请访问: http://127.0.0.1:9101?uri=http:/ /127.0.0.1:50548/vYt04KYoAA8= {"cod":"400","message":"Nothing to geocode"} 错误:异常:无法在 Object.throw_ [as throw] (http://localhost :50501/dart_sdk.js:5061:11) 在 getWeather (http://localhost:50501/packages/finalproject/Weatherproj/data_service.dart.lib.js:31:21) 在 getWeather.next () 在 http:// /localhost:50501/dart_sdk.js:38640:33 在 _RootZone.runUnary (http://localhost:50501/dart_sdk.js:38511:59) 在 _FutureListener.thenAwait.handleValue (http://localhost:50501/dart_sdk. js:33713:29) at handleValueCallback (http://localhost:50501/dart_sdk.js:34265:49) at Function._propagateToListeners (http://localhost:50501/dart_sdk.js:34303:17) at _Future.new .[_completeWithValue] (http://localhost:50501/dart_sdk.js:34151:23) 在 async._AsyncCallbackEntry.new.callback (http://loc alhost:50501/dart_sdk.js:34172:35) 在 Object._microtaskLoop (http://localhost:50501/dart_sdk.js:38778:13) 在 _startMicrotaskLoop (http://localhost:50501/dart_sdk.js:38788) 13) 在 http://localhost:50501/dart_sdk.js:34519:9
下面是模型类的代码:
class WeatherAppDetails {
City? city;
String? cod;
double? message;
int? cnt;
List<ListDetails>? list;
WeatherAppDetails(
{required this.city,
required this.cod,
required this.message,
required this.cnt,
required this.list});
WeatherAppDetails.fromJson(Map<String, dynamic> json) {
final cod = json['cod'];
final message = json['message'];
final cnt = json['cnt'];
if (json['list'] != null) {
list = <ListDetails>[];
json['list'].forEach((v) {
list!.add(ListDetails.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (city != null) {
data['city'] = city!.toJson();
}
data['cod'] = cod;
data['message'] = message;
data['cnt'] = cnt;
if (list != null) {
data['list'] = list!.map((v) => v.toJson()).toList();
}
return data;
}
}
class City {
int? id;
String? name;
Coord? coord;
String? country;
int? population;
int? timezone;
City(
{required this.id,
required this.name,
required this.coord,
required this.country,
required this.population,
required this.timezone});
City.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
coord = (json['coord'] != null ? Coord.fromJson(json['coord']) : null)!;
country = json['country'];
population = json['population'];
timezone = json['timezone'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['name'] = name;
if (coord != null) {
data['coord'] = coord!.toJson();
}
data['country'] = country;
data['population'] = population;
data['timezone'] = timezone;
return data;
}
}
class Coord {
double? lon;
double? lat;
Coord({required this.lon, required this.lat});
Coord.fromJson(Map<String, dynamic> json) {
lon = json['lon'];
lat = json['lat'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['lon'] = lon;
data['lat'] = lat;
return data;
}
}
class ListDetails {
int? dt;
int? sunrise;
int? sunset;
Temp? temp;
FeelsLike? feelsLike;
int? pressure;
int? humidity;
List<WeatherDetails>? weather;
double? speed;
int? deg;
double? gust;
int? clouds;
double? pop;
double? rain;
ListDetails(
{required this.dt,
required this.sunrise,
required this.sunset,
required this.temp,
required this.feelsLike,
required this.pressure,
required this.humidity,
required this.weather,
required this.speed,
required this.deg,
required this.gust,
required this.clouds,
required this.pop,
required this.rain});
ListDetails.fromJson(Map<String, dynamic> json) {
dt = json['dt'];
sunrise = json['sunrise'];
sunset = json['sunset'];
temp = json['temp'] != null ? Temp.fromJson(json['temp']) : null;
feelsLike = json['feels_like'] != null
? FeelsLike.fromJson(json['feels_like'])
: null;
pressure = json['pressure'];
humidity = json['humidity'];
if (json['weather'] != null) {
weather = <WeatherDetails>[];
json['weather'].forEach((v) {
weather!.add(WeatherDetails.fromJson(v));
});
}
speed = json['speed'];
deg = json['deg'];
gust = json['gust'];
clouds = json['clouds'];
pop = json['pop'];
rain = json['rain'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['dt'] = dt;
data['sunrise'] = sunrise;
data['sunset'] = sunset;
if (temp != null) {
data['temp'] = temp!.toJson();
}
if (feelsLike != null) {
data['feels_like'] = feelsLike!.toJson();
}
data['pressure'] = pressure;
data['humidity'] = humidity;
if (weather != null) {
data['weather'] = weather!.map((v) => v.toJson()).toList();
}
data['speed'] = speed;
data['deg'] = deg;
data['gust'] = gust;
data['clouds'] = clouds;
data['pop'] = pop;
data['rain'] = rain;
return data;
}
}
class Temp {
double? day;
double? min;
double? max;
double? night;
double? eve;
double? morn;
Temp(
{required this.day,
required this.min,
required this.max,
required this.night,
required this.eve,
required this.morn});
Temp.fromJson(Map<String, dynamic> json) {
day = json['day'];
min = json['min'];
max = json['max'];
night = json['night'];
eve = json['eve'];
morn = json['morn'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['day'] = day;
data['min'] = min;
data['max'] = max;
data['night'] = night;
data['eve'] = eve;
data['morn'] = morn;
return data;
}
}
class FeelsLike {
double? day;
double? night;
double? eve;
double? morn;
FeelsLike(
{required this.day,
required this.night,
required this.eve,
required this.morn});
FeelsLike.fromJson(Map<String, dynamic> json) {
day = json['day'];
night = json['night'];
eve = json['eve'];
morn = json['morn'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['day'] = day;
data['night'] = night;
data['eve'] = eve;
data['morn'] = morn;
return data;
}
}
class WeatherDetails {
int? id;
String? main;
String? description;
String? icon;
WeatherDetails(
{required this.id,
required this.main,
required this.description,
required this.icon});
WeatherDetails.fromJson(Map<String, dynamic> json) {
id = json['id'];
main = json['main'];
description = json['description'];
icon = json['icon'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['main'] = main;
data['description'] = description;
data['icon'] = icon;
return data;
}
}
下面是网络类的代码:
class DataService {
Future<WeatherAppDetails> getWeather(String name) async {
final queryParameters = {
'q': name,
'appid': '3c044b7295d2df14f8bee74c19d4b96f',
'units': 'imperial',
'cnt': '7'
};
final uri = Uri.https(
'api.openweathermap.org', '/data/2.5/forecast/daily', queryParameters);
final response = await http.get(uri);
print(response.body);
if (response.statusCode == 200) {
return WeatherAppDetails.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to get posts');
}
}
}
基本上 api.weather 告诉你“q”参数的给定值不存在,没有找到地理位置。
添加城市参数输入很有帮助
您在请求中输入的城市是错误的,这应该有效(示例)
https://api.openweathermap.org/data/2.5/forecast/daily?appid=3c044b7295d2df14f8bee74c19d4b96f&q=英国伦敦
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.