I am having this problem converting the following json to TodayWeather entity. How can I use the named constructor TodayHours
?
I’ve been looking for a solution for this issue for a few days now, but I haven’t found much, please guide me.
JSON:
{
"days": [
{
"datetime": "2023-01-05",
"datetimeEpoch": 1672864200,
"tempmax": 8.8,
"tempmin": 2.3,
"temp": 5.3,
"windspeed": 8.6,
"winddir": 223.9,
"visibility": 19.7,
"sunrise": "06:43:43",
"sunset": "16:30:24",
"conditions": "Snow, Rain, Overcast",
"hours": [
{
"datetime": "00:00:00",
"datetimeEpoch": 1672864200,
"temp": 4.4,
"humidity": 27.65,
"windspeed": 6.5,
"winddir": 249.2,
"visibility": 24.1,
"conditions": "Partially cloudy"
},
{
"datetime": "01:00:00",
"datetimeEpoch": 1672864200,
"temp": 4.4,
"humidity": 27.65,
"windspeed": 6.5,
"winddir": 249.2,
"visibility": 24.1,
"conditions": "Partially cloudy"
}
]
}
]
}
And my TodayWeather
entity is:
class TodayWeather {
final String datetime;
final num dateEpoch;
final String conditions;
final num tempMax;
final num tempMin;
final num windDir;
final num windSpeed;
final String sunRise;
final String sunSet;
final num humidity;
final num visibility;
final List<TodayHourse> hours;
TodayWeather.fromJson(Map<String, dynamic> json)
: datetime = json['days'][0]['datetime'],
dateEpoch = json['days'][0]['datetimeEpoch'],
conditions = json['days'][0]['conditions'],
tempMax = json['days'][0]['tempmax'],
tempMin = json['days'][0]['tempmin'],
windDir = json['days'][0]['winddir'],
windSpeed = json['days'][0]['windspeed'],
sunRise = json['days'][0]['sunrise'],
sunSet = json['days'][0]['sunset'],
humidity = json['days'][0]['humidity'],
visibility = json['days'][0]['visibility'],
hours = List<TodayHourse>.from(
json['days'][0]['hours'].map((x) => x.toJson())).toList();
}
This is my TodayHours
entity:
class TodayHourse {
final String datetime;
final num dateEpoch;
final String conditions;
final num temp;
final num windDir;
final num windSpeed;
final num humidity;
final num visibility;
Map<String, dynamic> toJson() => {
'datetime': datetime,
'datetimeEpoch': dateEpoch,
'conditions': conditions,
'temp': temp,
'winddir': windDir,
'windspeed': windSpeed,
'humidity': humidity,
'visibility': visibility
};
TodayHourse.fromJson(Map<String, dynamic> json)
: datetime = json['days'][0]['datetime'],
dateEpoch = json['days'][0]['datetimeEpoch'],
conditions = json['days'][0]['conditions'],
temp = json['days'][0]['temp'],
windDir = json['days'][0]['winddir'],
windSpeed = json['days'][0]['windspeed'],
humidity = json['days'][0]['humidity'],
visibility = json['days'][0]['visibility'];
}
This method is parsing Json to TodayWeather
:
@override
Future<TodayWeather> getTodayWeather() async {
final response = await httpClient.get(
'36.31559%2C59.56796/today?unitGroup=metric&key=Key&contentType=json');
validResponse(response);
return TodayWeather.fromJson(response.data);
}
3
Answers
First change your
TodayHourse.fromJson
to this:your
hours
is list ofMap
and you don’t need to usejson['days'][0]
. Then in yourTodayWeather.fromJson
, changehours
to this:you are using wrong function instead of
toJson
, you need to callTodayHourse.fromJson(x)
.Try to use this model:
First of all for more clear way to parse
JSON
arrays you can create helper method in your models, I call itModel.fromJsonList
So by this method you can parse array response very easy and clean for example:
Totally we have:
Note
: I didn’t parse all variable, do it by yourself.