I am making a simple get request to the OpenWeather API but while debugging I realized that despite I am getting response the mapping process of body values is not done. The debugger is not entering my data model and therefore is not mapping anything and I am not able to save data into my "weatherList" list.
Here is my get request:
fetchWeatherData(urlw) async {
final responsew = await client.get(Uri.parse(urlw));
if (responsew.statusCode == 200) {
weatherList = (json.decode(responsew.body) as List)
.map((data) => weatherAPI.fromJson(data))
.toList();
} else {
throw Exception('Failed to load data');
}
}
here is the response.body values:
{"coord":{"lon":2.1734,"lat":41.3851},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01d"}],"base":"stations","main":{"temp":21.23,"feels_like":20.56,"temp_min":18.16,"temp_max":26.64,"pressure":1016,"humidity":44},"visibility":10000,"wind":{"speed":7.72,"deg":80},"clouds":{"all":0},"dt":1649941625,"sys":{"type":2,"id":2003688,"country":"ES","sunrise":1649913201,"sunset":1649960967},"timezone":7200,"id":6544100,"name":"Eixample","cod":200}
and here is my data model:
class weatherAPI {
Coord? _coord;
List<Weather>? _weather;
String? _base;
MainWeather? _main;
int? _visibility;
Wind? _wind;
Clouds? _clouds;
int? _dt;
Sys? _sys;
int? _timezone;
int? _id;
String? _name;
int? _cod;
weatherAPI(
{Coord? coord,
List<Weather>? weather,
String? base,
MainWeather? main,
int? visibility,
Wind? wind,
Clouds? clouds,
int? dt,
Sys? sys,
int? timezone,
int? id,
String? name,
int? cod}) {
if (coord != null) {
this._coord = coord;
}
if (weather != null) {
this._weather = weather;
}
if (base != null) {
this._base = base;
}
if (main != null) {
this._main = main;
}
if (visibility != null) {
this._visibility = visibility;
}
if (wind != null) {
this._wind = wind;
}
if (clouds != null) {
this._clouds = clouds;
}
if (dt != null) {
this._dt = dt;
}
if (sys != null) {
this._sys = sys;
}
if (timezone != null) {
this._timezone = timezone;
}
if (id != null) {
this._id = id;
}
if (name != null) {
this._name = name;
}
if (cod != null) {
this._cod = cod;
}
}
Coord? get coord => _coord;
set coord(Coord? coord) => _coord = coord;
List<Weather>? get weather => _weather;
set weather(List<Weather>? weather) => _weather = weather;
String? get base => _base;
set base(String? base) => _base = base;
MainWeather? get main => _main;
set main(MainWeather? main) => _main = main;
int? get visibility => _visibility;
set visibility(int? visibility) => _visibility = visibility;
Wind? get wind => _wind;
set wind(Wind? wind) => _wind = wind;
Clouds? get clouds => _clouds;
set clouds(Clouds? clouds) => _clouds = clouds;
int? get dt => _dt;
set dt(int? dt) => _dt = dt;
Sys? get sys => _sys;
set sys(Sys? sys) => _sys = sys;
int? get timezone => _timezone;
set timezone(int? timezone) => _timezone = timezone;
int? get id => _id;
set id(int? id) => _id = id;
String? get name => _name;
set name(String? name) => _name = name;
int? get cod => _cod;
set cod(int? cod) => _cod = cod;
factory weatherAPI.fromJson(Map<String, dynamic> json) {
return weatherAPI(
coord: json['coord'] != null ? Coord.fromJson(json['coord']) : null,
//weather: json['weather'] != null ? Weather.fromJson(json['weather']) : null,
base : json['base'],
main : json['main'] != null ? MainWeather.fromJson(json['main']) : null,
visibility : json['visibility'],
wind : json['wind'] != null ? Wind.fromJson(json['wind']) : null,
clouds : json['clouds'] != null ? Clouds.fromJson(json['clouds']) : null,
dt : json['dt'],
sys : json['sys'] != null ? Sys.fromJson(json['sys']) : null,
timezone : json['timezone'],
id : json['id'],
name : json['name'],
cod : json['cod']
);
}
Is there any parsing error I am making??
CodePudding user response:
Try this model once
// To parse this JSON data, do
import 'dart:convert';
class WeatherAPI {
WeatherAPI({
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,
});
Coord coord;
List<Weather> weather;
String base;
Main main;
int visibility;
Wind wind;
Clouds clouds;
int dt;
Sys sys;
int timezone;
int id;
String name;
int cod;
factory WeatherAPI.fromJson(String str) => WeatherAPI.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory WeatherAPI.fromMap(Map<String, dynamic> json) => WeatherAPI(
coord: json["coord"] == null ? null : Coord.fromMap(json["coord"]),
weather: json["weather"] == null ? null : List<Weather>.from(json["weather"].map((x) => Weather.fromMap(x))),
base: json["base"] == null ? null : json["base"],
main: json["main"] == null ? null : Main.fromMap(json["main"]),
visibility: json["visibility"] == null ? null : json["visibility"],
wind: json["wind"] == null ? null : Wind.fromMap(json["wind"]),
clouds: json["clouds"] == null ? null : Clouds.fromMap(json["clouds"]),
dt: json["dt"] == null ? null : json["dt"],
sys: json["sys"] == null ? null : Sys.fromMap(json["sys"]),
timezone: json["timezone"] == null ? null : json["timezone"],
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
cod: json["cod"] == null ? null : json["cod"],
);
Map<String, dynamic> toMap() => {
"coord": coord == null ? null : coord.toMap(),
"weather": weather == null ? null : List<dynamic>.from(weather.map((x) => x.toMap())),
"base": base == null ? null : base,
"main": main == null ? null : main.toMap(),
"visibility": visibility == null ? null : visibility,
"wind": wind == null ? null : wind.toMap(),
"clouds": clouds == null ? null : clouds.toMap(),
"dt": dt == null ? null : dt,
"sys": sys == null ? null : sys.toMap(),
"timezone": timezone == null ? null : timezone,
"id": id == null ? null : id,
"name": name == null ? null : name,
"cod": cod == null ? null : cod,
};
}
class Clouds {
Clouds({
this.all,
});
int all;
factory Clouds.fromJson(String str) => Clouds.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Clouds.fromMap(Map<String, dynamic> json) => Clouds(
all: json["all"] == null ? null : json["all"],
);
Map<String, dynamic> toMap() => {
"all": all == null ? null : all,
};
}
class Coord {
Coord({
this.lon,
this.lat,
});
double lon;
double lat;
factory Coord.fromJson(String str) => Coord.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Coord.fromMap(Map<String, dynamic> json) => Coord(
lon: json["lon"] == null ? null : json["lon"].toDouble(),
lat: json["lat"] == null ? null : json["lat"].toDouble(),
);
Map<String, dynamic> toMap() => {
"lon": lon == null ? null : lon,
"lat": lat == null ? null : lat,
};
}
class Main {
Main({
this.temp,
this.feelsLike,
this.tempMin,
this.tempMax,
this.pressure,
this.humidity,
});
double temp;
double feelsLike;
double tempMin;
double tempMax;
int pressure;
int humidity;
factory Main.fromJson(String str) => Main.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Main.fromMap(Map<String, dynamic> json) => Main(
temp: json["temp"] == null ? null : json["temp"].toDouble(),
feelsLike: json["feels_like"] == null ? null : json["feels_like"].toDouble(),
tempMin: json["temp_min"] == null ? null : json["temp_min"].toDouble(),
tempMax: json["temp_max"] == null ? null : json["temp_max"].toDouble(),
pressure: json["pressure"] == null ? null : json["pressure"],
humidity: json["humidity"] == null ? null : json["humidity"],
);
Map<String, dynamic> toMap() => {
"temp": temp == null ? null : temp,
"feels_like": feelsLike == null ? null : feelsLike,
"temp_min": tempMin == null ? null : tempMin,
"temp_max": tempMax == null ? null : tempMax,
"pressure": pressure == null ? null : pressure,
"humidity": humidity == null ? null : humidity,
};
}
class Sys {
Sys({
this.type,
this.id,
this.country,
this.sunrise,
this.sunset,
});
int type;
int id;
String country;
int sunrise;
int sunset;
factory Sys.fromJson(String str) => Sys.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Sys.fromMap(Map<String, dynamic> json) => Sys(
type: json["type"] == null ? null : json["type"],
id: json["id"] == null ? null : json["id"],
country: json["country"] == null ? null : json["country"],
sunrise: json["sunrise"] == null ? null : json["sunrise"],
sunset: json["sunset"] == null ? null : json["sunset"],
);
Map<String, dynamic> toMap() => {
"type": type == null ? null : type,
"id": id == null ? null : id,
"country": country == null ? null : country,
"sunrise": sunrise == null ? null : sunrise,
"sunset": sunset == null ? null : sunset,
};
}
class Weather {
Weather({
this.id,
this.main,
this.description,
this.icon,
});
int id;
String main;
String description;
String icon;
factory Weather.fromJson(String str) => Weather.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Weather.fromMap(Map<String, dynamic> json) => Weather(
id: json["id"] == null ? null : json["id"],
main: json["main"] == null ? null : json["main"],
description: json["description"] == null ? null : json["description"],
icon: json["icon"] == null ? null : json["icon"],
);
Map<String, dynamic> toMap() => {
"id": id == null ? null : id,
"main": main == null ? null : main,
"description": description == null ? null : description,
"icon": icon == null ? null : icon,
};
}
class Wind {
Wind({
this.speed,
this.deg,
});
double speed;
int deg;
factory Wind.fromJson(String str) => Wind.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Wind.fromMap(Map<String, dynamic> json) => Wind(
speed: json["speed"] == null ? null : json["speed"].toDouble(),
deg: json["deg"] == null ? null : json["deg"],
);
Map<String, dynamic> toMap() => {
"speed": speed == null ? null : speed,
"deg": deg == null ? null : deg,
};
}