jsonflutterapidartparsing

Parse Json in Dart/Flutter to Entity/Model


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.

enter image description here

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);
  }

Solution

  • First change your TodayHourse.fromJson to this:

    TodayHourse.fromJson(Map<String, dynamic> json)
          : datetime = json['datetime'],
            dateEpoch = json['datetimeEpoch'],
            conditions = json['conditions'],
            temp = json['temp'],
            windDir = json['winddir'],
            windSpeed = json['windspeed'],
            humidity = json['humidity'],
            visibility = json['visibility'];
    

    your hours is list of Map and you don't need to use json['days'][0]. Then in your TodayWeather.fromJson, change hours to this:

    hours = (json['days'][0]['hours'] as List).map((x) => TodayHourse.fromJson(x)).toList();
    

    you are using wrong function instead of toJson, you need to call TodayHourse.fromJson(x).