flutter Weatherapi.com 每天和每小时获取天气预报,而不仅仅是当前

问题描述 投票:0回答:1

我制作模型从 url 天气 api 获取数据 但我无法获取当天的天气预报列表,并且当天的列表还有另一个小时列表 当我制作 futurebuilder 时,我当前的模型工作得很好, 但我怎样才能得到当天的预报列表和小时列表的数据

所以我需要列出日期和时间列表

型号

class Weather {
  var cityName;
  var icon;
  var condition;
  var temp;
  var wind;
  var humidity;
  var local_time;
  var hours;
  Weather(
      {required this.cityName,
      required this.icon,
      required this.condition,
      required this.temp,
      required this.wind,
      required this.humidity,
      required this.local_time,
      required this.hours});
  Weather.fromJson(Map<String, dynamic> json) {
    cityName = json["location"]["name"];
    icon = json["current"]["condition"]["icon"];
    condition = json["current"]["condition"]["text"];
    temp = json["current"]["temp_c"];
    wind = json["current"]['wind_kph'];
    humidity = json["current"]['humidity'];
    local_time = json["location"]['localtime'];
  }
}

获取数据类

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'weather_model.dart';

class weatherData {
  Future<Weather> getData() async {
    var uriCall = Uri.parse(
        "https://api.weatherapi.com/v1/forecast.json?key=******&q=ramadi,iq&days=3&lang=ar");
    var response = await http.get(uriCall);
    var bodyDecode = utf8.decode(response.bodyBytes);
    var body = jsonDecode(bodyDecode);
    return Weather.fromJson(body);
  }
}

响应正文

{
    "location": {
        "name": "London",
        "region": "City of London, Greater London",
        "country": "United Kingdom",
        "lat": 51.52,
        "lon": -0.11,
        "tz_id": "Europe/London",
        "localtime_epoch": 1693161422,
        "localtime": "2023-08-27 19:37"
    },
    "current": {
        "last_updated_epoch": 1693161000,
        "last_updated": "2023-08-27 19:30",
        "temp_c": 17.0,
        "temp_f": 62.6,
        "is_day": 1,
        "condition": {
            "text": "Partly cloudy",
            "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
            "code": 1003
        },
        "wind_mph": 8.1,
        "wind_kph": 13.0,
        "wind_degree": 300,
        "wind_dir": "WNW",
        "pressure_mb": 1011.0,
        "pressure_in": 29.85,
        "precip_mm": 0.1,
        "precip_in": 0.0,
        "humidity": 59,
        "cloud": 50,
        "feelslike_c": 17.0,
        "feelslike_f": 62.6,
        "vis_km": 10.0,
        "vis_miles": 6.0,
        "uv": 4.0,
        "gust_mph": 11.9,
        "gust_kph": 19.1
    },
    "forecast": {
        "forecastday": [
            {
                "date": "2023-08-27",
                "date_epoch": 1693094400,
                "day": {
                    "maxtemp_c": 19.5,
                    "maxtemp_f": 67.1,
                    "mintemp_c": 11.4,
                    "mintemp_f": 52.5,
                    "avgtemp_c": 15.8,
                    "avgtemp_f": 60.4,
                    "maxwind_mph": 11.2,
                    "maxwind_kph": 18.0,
                    "totalprecip_mm": 1.4,
                    "totalprecip_in": 0.06,
                    "totalsnow_cm": 0.0,
                    "avgvis_km": 10.0,
                    "avgvis_miles": 6.0,
                    "avghumidity": 70.0,
                    "daily_will_it_rain": 1,
                    "daily_chance_of_rain": 88,
                    "daily_will_it_snow": 0,
                    "daily_chance_of_snow": 0,
                    "condition": {
                        "text": "Patchy rain possible",
                        "icon": "//cdn.weatherapi.com/weather/64x64/day/176.png",
                        "code": 1063
                    },
                    "uv": 4.0
                },
                "astro": {
                    "sunrise": "06:04 AM",
                    "sunset": "07:59 PM",
                    "moonrise": "06:38 PM",
                    "moonset": "12:18 AM",
                    "moon_phase": "Waxing Gibbous",
                    "moon_illumination": "77",
                    "is_moon_up": 0,
                    "is_sun_up": 0
                },
                "hour": [
                    
                    {
                        "time_epoch": 1693116000,
                        "time": "2023-08-27 07:00",
                        "temp_c": 12.7,
                        "temp_f": 54.9,
                        "is_day": 1,
                        "condition": {
                            "text": "Sunny",
                            "icon": "//cdn.weatherapi.com/weather/64x64/day/113.png",
                            "code": 1000
                        },
                        "wind_mph": 8.1,
                        "wind_kph": 13.0,
                        "wind_degree": 285,
                        "wind_dir": "WNW",
                        "pressure_mb": 1010.0,
                        "pressure_in": 29.81,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 84,
                        "cloud": 24,
                        "feelslike_c": 11.5,
                        "feelslike_f": 52.7,
                        "windchill_c": 11.5,
                        "windchill_f": 52.7,
                        "heatindex_c": 12.7,
                        "heatindex_f": 54.9,
                        "dewpoint_c": 10.0,
                        "dewpoint_f": 50.0,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 10.5,
                        "gust_kph": 16.9,
                        "uv": 4.0
                    },
                    {
                        "time_epoch": 1693141200,
                        "time": "2023-08-27 14:00",
                        "temp_c": 19.3,
                        "temp_f": 66.7,
                        "is_day": 1,
                        "condition": {
                            "text": "Overcast",
                            "icon": "//cdn.weatherapi.com/weather/64x64/day/122.png",
                            "code": 1009
                        },
                        "wind_mph": 11.2,
                        "wind_kph": 18.0,
                        "wind_degree": 284,
                        "wind_dir": "WNW",
                        "pressure_mb": 1010.0,
                        "pressure_in": 29.82,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 51,
                        "cloud": 96,
                        "feelslike_c": 19.3,
                        "feelslike_f": 66.7,
                        "windchill_c": 19.3,
                        "windchill_f": 66.7,
                        "heatindex_c": 19.3,
                        "heatindex_f": 66.7,
                        "dewpoint_c": 8.9,
                        "dewpoint_f": 48.0,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 13.2,
                        "gust_kph": 21.2,
                        "uv": 4.0
                    },
                    {
                        "time_epoch": 1693144800,
                        "time": "2023-08-27 15:00",
                        "temp_c": 19.4,
                        "temp_f": 66.9,
                        "is_day": 1,
                        "condition": {
                            "text": "Patchy rain possible",
                            "icon": "//cdn.weatherapi.com/weather/64x64/day/176.png",
                            "code": 1063
                        },
                        "wind_mph": 10.5,
                        "wind_kph": 16.9,
                        "wind_degree": 286,
                        "wind_dir": "WNW",
                        "pressure_mb": 1010.0,
                        "pressure_in": 29.82,
                        "precip_mm": 0.1,
                        "precip_in": 0.0,
                        "humidity": 52,
                        "cloud": 100,
                        "feelslike_c": 19.4,
                        "feelslike_f": 66.9,
                        "windchill_c": 19.4,
                        "windchill_f": 66.9,
                        "heatindex_c": 19.4,
                        "heatindex_f": 66.9,
                        "dewpoint_c": 9.3,
                        "dewpoint_f": 48.7,
                        "will_it_rain": 0,
                        "chance_of_rain": 59,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 12.3,
                        "gust_kph": 19.8,
                        "uv": 4.0
                    },
                    {
                        "time_epoch": 1693148400,
                        "time": "2023-08-27 16:00",
                        "temp_c": 18.9,
                        "temp_f": 66.0,
                        "is_day": 1,
                        "condition": {
                            "text": "Cloudy",
                            "icon": "//cdn.weatherapi.com/weather/64x64/day/119.png",
                            "code": 1006
                        },
                        "wind_mph": 11.0,
                        "wind_kph": 17.6,
                        "wind_degree": 291,
                        "wind_dir": "WNW",
                        "pressure_mb": 1010.0,
                        "pressure_in": 29.83,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 55,
                        "cloud": 73,
                        "feelslike_c": 18.9,
                        "feelslike_f": 66.0,
                        "windchill_c": 18.9,
                        "windchill_f": 66.0,
                        "heatindex_c": 18.9,
                        "heatindex_f": 66.0,
                        "dewpoint_c": 9.7,
                        "dewpoint_f": 49.5,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 13.6,
                        "gust_kph": 22.0,
                        "uv": 4.0
                    },
                    {
                        "time_epoch": 1693152000,
                        "time": "2023-08-27 17:00",
                        "temp_c": 18.8,
                        "temp_f": 65.8,
                        "is_day": 1,
                        "condition": {
                            "text": "Partly cloudy",
                            "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
                            "code": 1003
                        },
                        "wind_mph": 9.2,
                        "wind_kph": 14.8,
                        "wind_degree": 297,
                        "wind_dir": "WNW",
                        "pressure_mb": 1011.0,
                        "pressure_in": 29.84,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 54,
                        "cloud": 49,
                        "feelslike_c": 18.8,
                        "feelslike_f": 65.8,
                        "windchill_c": 18.8,
                        "windchill_f": 65.8,
                        "heatindex_c": 18.8,
                        "heatindex_f": 65.8,
                        "dewpoint_c": 9.3,
                        "dewpoint_f": 48.7,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 11.6,
                        "gust_kph": 18.7,
                        "uv": 5.0
                    },
                    {
                        "time_epoch": 1693155600,
                        "time": "2023-08-27 18:00",
                        "temp_c": 18.2,
                        "temp_f": 64.8,
                        "is_day": 1,
                        "condition": {
                            "text": "Cloudy",
                            "icon": "//cdn.weatherapi.com/weather/64x64/day/119.png",
                            "code": 1006
                        },
                        "wind_mph": 7.4,
                        "wind_kph": 11.9,
                        "wind_degree": 288,
                        "wind_dir": "WNW",
                        "pressure_mb": 1011.0,
                        "pressure_in": 29.85,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 58,
                        "cloud": 86,
                        "feelslike_c": 18.2,
                        "feelslike_f": 64.8,
                        "windchill_c": 18.2,
                        "windchill_f": 64.8,
                        "heatindex_c": 18.2,
                        "heatindex_f": 64.8,
                        "dewpoint_c": 9.8,
                        "dewpoint_f": 49.6,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 10.3,
                        "gust_kph": 16.6,
                        "uv": 4.0
                    },
                    {
                        "time_epoch": 1693159200,
                        "time": "2023-08-27 19:00",
                        "temp_c": 17.0,
                        "temp_f": 62.6,
                        "is_day": 1,
                        "condition": {
                            "text": "Partly cloudy",
                            "icon": "//cdn.weatherapi.com/weather/64x64/day/116.png",
                            "code": 1003
                        },
                        "wind_mph": 8.1,
                        "wind_kph": 13.0,
                        "wind_degree": 300,
                        "wind_dir": "WNW",
                        "pressure_mb": 1011.0,
                        "pressure_in": 29.85,
                        "precip_mm": 0.1,
                        "precip_in": 0.0,
                        "humidity": 59,
                        "cloud": 50,
                        "feelslike_c": 17.3,
                        "feelslike_f": 63.1,
                        "windchill_c": 17.3,
                        "windchill_f": 63.1,
                        "heatindex_c": 17.3,
                        "heatindex_f": 63.1,
                        "dewpoint_c": 9.8,
                        "dewpoint_f": 49.6,
                        "will_it_rain": 1,
                        "chance_of_rain": 88,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 11.9,
                        "gust_kph": 19.1,
                        "uv": 4.0
                    },
                    {
                        "time_epoch": 1693162800,
                        "time": "2023-08-27 20:00",
                        "temp_c": 16.9,
                        "temp_f": 62.4,
                        "is_day": 0,
                        "condition": {
                            "text": "Cloudy",
                            "icon": "//cdn.weatherapi.com/weather/64x64/night/119.png",
                            "code": 1006
                        },
                        "wind_mph": 7.4,
                        "wind_kph": 11.9,
                        "wind_degree": 279,
                        "wind_dir": "W",
                        "pressure_mb": 1012.0,
                        "pressure_in": 29.88,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 65,
                        "cloud": 74,
                        "feelslike_c": 16.9,
                        "feelslike_f": 62.4,
                        "windchill_c": 16.9,
                        "windchill_f": 62.4,
                        "heatindex_c": 16.9,
                        "heatindex_f": 62.4,
                        "dewpoint_c": 10.2,
                        "dewpoint_f": 50.4,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 10.5,
                        "gust_kph": 16.9,
                        "uv": 1.0
                    },
                    {
                        "time_epoch": 1693166400,
                        "time": "2023-08-27 21:00",
                        "temp_c": 16.2,
                        "temp_f": 61.2,
                        "is_day": 0,
                        "condition": {
                            "text": "Patchy rain possible",
                            "icon": "//cdn.weatherapi.com/weather/64x64/night/176.png",
                            "code": 1063
                        },
                        "wind_mph": 7.8,
                        "wind_kph": 12.6,
                        "wind_degree": 280,
                        "wind_dir": "W",
                        "pressure_mb": 1012.0,
                        "pressure_in": 29.88,
                        "precip_mm": 0.1,
                        "precip_in": 0.0,
                        "humidity": 70,
                        "cloud": 75,
                        "feelslike_c": 16.2,
                        "feelslike_f": 61.2,
                        "windchill_c": 16.2,
                        "windchill_f": 61.2,
                        "heatindex_c": 16.2,
                        "heatindex_f": 61.2,
                        "dewpoint_c": 10.7,
                        "dewpoint_f": 51.3,
                        "will_it_rain": 0,
                        "chance_of_rain": 64,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 11.0,
                        "gust_kph": 17.6,
                        "uv": 1.0
                    },
                    {
                        "time_epoch": 1693170000,
                        "time": "2023-08-27 22:00",
                        "temp_c": 15.7,
                        "temp_f": 60.3,
                        "is_day": 0,
                        "condition": {
                            "text": "Overcast",
                            "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
                            "code": 1009
                        },
                        "wind_mph": 7.2,
                        "wind_kph": 11.5,
                        "wind_degree": 287,
                        "wind_dir": "WNW",
                        "pressure_mb": 1012.0,
                        "pressure_in": 29.9,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 72,
                        "cloud": 100,
                        "feelslike_c": 15.7,
                        "feelslike_f": 60.3,
                        "windchill_c": 15.7,
                        "windchill_f": 60.3,
                        "heatindex_c": 15.7,
                        "heatindex_f": 60.3,
                        "dewpoint_c": 10.8,
                        "dewpoint_f": 51.4,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 9.8,
                        "gust_kph": 15.8,
                        "uv": 1.0
                    },
                    {
                        "time_epoch": 1693173600,
                        "time": "2023-08-27 23:00",
                        "temp_c": 15.5,
                        "temp_f": 59.9,
                        "is_day": 0,
                        "condition": {
                            "text": "Overcast",
                            "icon": "//cdn.weatherapi.com/weather/64x64/night/122.png",
                            "code": 1009
                        },
                        "wind_mph": 6.7,
                        "wind_kph": 10.8,
                        "wind_degree": 299,
                        "wind_dir": "WNW",
                        "pressure_mb": 1013.0,
                        "pressure_in": 29.9,
                        "precip_mm": 0.0,
                        "precip_in": 0.0,
                        "humidity": 72,
                        "cloud": 100,
                        "feelslike_c": 15.5,
                        "feelslike_f": 59.9,
                        "windchill_c": 15.5,
                        "windchill_f": 59.9,
                        "heatindex_c": 15.5,
                        "heatindex_f": 59.9,
                        "dewpoint_c": 10.5,
                        "dewpoint_f": 50.9,
                        "will_it_rain": 0,
                        "chance_of_rain": 0,
                        "will_it_snow": 0,
                        "chance_of_snow": 0,
                        "vis_km": 10.0,
                        "vis_miles": 6.0,
                        "gust_mph": 9.2,
                        "gust_kph": 14.8,
                        "uv": 1.0
                    }
                ]
            }
        ]
    }
}
json flutter list api weather
1个回答
0
投票

'''

import 'package:meta/meta.dart';
import 'dart:convert';

class HomeCardModel {
    final Location location;
    final Current current;
    final Forecast forecast;

    HomeCardModel({
        required this.location,
        required this.current,
        required this.forecast,
    });

    factory HomeCardModel.fromRawJson(String str) => HomeCardModel.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory HomeCardModel.fromJson(Map<String, dynamic> json) => HomeCardModel(
        location: Location.fromJson(json["location"]),
        current: Current.fromJson(json["current"]),
        forecast: Forecast.fromJson(json["forecast"]),
    );

    Map<String, dynamic> toJson() => {
        "location": location.toJson(),
        "current": current.toJson(),
        "forecast": forecast.toJson(),
    };
}

class Current {
    final int lastUpdatedEpoch;
    final String lastUpdated;
    final int tempC;
    final double tempF;
    final int isDay;
    final Condition condition;
    final double windMph;
    final int windKph;
    final int windDegree;
    final WindDir windDir;
    final int pressureMb;
    final double pressureIn;
    final double precipMm;
    final int precipIn;
    final int humidity;
    final int cloud;
    final int feelslikeC;
    final double feelslikeF;
    final int visKm;
    final int visMiles;
    final int uv;
    final double gustMph;
    final double gustKph;

    Current({
        required this.lastUpdatedEpoch,
        required this.lastUpdated,
        required this.tempC,
        required this.tempF,
        required this.isDay,
        required this.condition,
        required this.windMph,
        required this.windKph,
        required this.windDegree,
        required this.windDir,
        required this.pressureMb,
        required this.pressureIn,
        required this.precipMm,
        required this.precipIn,
        required this.humidity,
        required this.cloud,
        required this.feelslikeC,
        required this.feelslikeF,
        required this.visKm,
        required this.visMiles,
        required this.uv,
        required this.gustMph,
        required this.gustKph,
    });

    factory Current.fromRawJson(String str) => Current.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Current.fromJson(Map<String, dynamic> json) => Current(
        lastUpdatedEpoch: json["last_updated_epoch"],
        lastUpdated: json["last_updated"],
        tempC: json["temp_c"],
        tempF: json["temp_f"]?.toDouble(),
        isDay: json["is_day"],
        condition: Condition.fromJson(json["condition"]),
        windMph: json["wind_mph"]?.toDouble(),
        windKph: json["wind_kph"],
        windDegree: json["wind_degree"],
        windDir: windDirValues.map[json["wind_dir"]]!,
        pressureMb: json["pressure_mb"],
        pressureIn: json["pressure_in"]?.toDouble(),
        precipMm: json["precip_mm"]?.toDouble(),
        precipIn: json["precip_in"],
        humidity: json["humidity"],
        cloud: json["cloud"],
        feelslikeC: json["feelslike_c"],
        feelslikeF: json["feelslike_f"]?.toDouble(),
        visKm: json["vis_km"],
        visMiles: json["vis_miles"],
        uv: json["uv"],
        gustMph: json["gust_mph"]?.toDouble(),
        gustKph: json["gust_kph"]?.toDouble(),
    );

    Map<String, dynamic> toJson() => {
        "last_updated_epoch": lastUpdatedEpoch,
        "last_updated": lastUpdated,
        "temp_c": tempC,
        "temp_f": tempF,
        "is_day": isDay,
        "condition": condition.toJson(),
        "wind_mph": windMph,
        "wind_kph": windKph,
        "wind_degree": windDegree,
        "wind_dir": windDirValues.reverse[windDir],
        "pressure_mb": pressureMb,
        "pressure_in": pressureIn,
        "precip_mm": precipMm,
        "precip_in": precipIn,
        "humidity": humidity,
        "cloud": cloud,
        "feelslike_c": feelslikeC,
        "feelslike_f": feelslikeF,
        "vis_km": visKm,
        "vis_miles": visMiles,
        "uv": uv,
        "gust_mph": gustMph,
        "gust_kph": gustKph,
    };
}

class Condition {
    final String text;
    final String icon;
    final int code;

    Condition({
        required this.text,
        required this.icon,
        required this.code,
    });

    factory Condition.fromRawJson(String str) => Condition.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Condition.fromJson(Map<String, dynamic> json) => Condition(
        text: json["text"],
        icon: json["icon"],
        code: json["code"],
    );

    Map<String, dynamic> toJson() => {
        "text": text,
        "icon": icon,
        "code": code,
    };
}

enum WindDir {
    W,
    WNW
}

final windDirValues = EnumValues({
    "W": WindDir.W,
    "WNW": WindDir.WNW
});

class Forecast {
    final List<Forecastday> forecastday;

    Forecast({
        required this.forecastday,
    });

    factory Forecast.fromRawJson(String str) => Forecast.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Forecast.fromJson(Map<String, dynamic> json) => Forecast(
        forecastday: List<Forecastday>.from(json["forecastday"].map((x) => Forecastday.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "forecastday": List<dynamic>.from(forecastday.map((x) => x.toJson())),
    };
}

class Forecastday {
    final DateTime date;
    final int dateEpoch;
    final Day day;
    final Astro astro;
    final List<Hour> hour;

    Forecastday({
        required this.date,
        required this.dateEpoch,
        required this.day,
        required this.astro,
        required this.hour,
    });

    factory Forecastday.fromRawJson(String str) => Forecastday.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Forecastday.fromJson(Map<String, dynamic> json) => Forecastday(
        date: DateTime.parse(json["date"]),
        dateEpoch: json["date_epoch"],
        day: Day.fromJson(json["day"]),
        astro: Astro.fromJson(json["astro"]),
        hour: List<Hour>.from(json["hour"].map((x) => Hour.fromJson(x))),
    );

    Map<String, dynamic> toJson() => {
        "date": "${date.year.toString().padLeft(4, '0')}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}",
        "date_epoch": dateEpoch,
        "day": day.toJson(),
        "astro": astro.toJson(),
        "hour": List<dynamic>.from(hour.map((x) => x.toJson())),
    };
}

class Astro {
    final String sunrise;
    final String sunset;
    final String moonrise;
    final String moonset;
    final String moonPhase;
    final String moonIllumination;
    final int isMoonUp;
    final int isSunUp;

    Astro({
        required this.sunrise,
        required this.sunset,
        required this.moonrise,
        required this.moonset,
        required this.moonPhase,
        required this.moonIllumination,
        required this.isMoonUp,
        required this.isSunUp,
    });

    factory Astro.fromRawJson(String str) => Astro.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Astro.fromJson(Map<String, dynamic> json) => Astro(
        sunrise: json["sunrise"],
        sunset: json["sunset"],
        moonrise: json["moonrise"],
        moonset: json["moonset"],
        moonPhase: json["moon_phase"],
        moonIllumination: json["moon_illumination"],
        isMoonUp: json["is_moon_up"],
        isSunUp: json["is_sun_up"],
    );

    Map<String, dynamic> toJson() => {
        "sunrise": sunrise,
        "sunset": sunset,
        "moonrise": moonrise,
        "moonset": moonset,
        "moon_phase": moonPhase,
        "moon_illumination": moonIllumination,
        "is_moon_up": isMoonUp,
        "is_sun_up": isSunUp,
    };
}

class Day {
    final double maxtempC;
    final double maxtempF;
    final double mintempC;
    final double mintempF;
    final double avgtempC;
    final double avgtempF;
    final double maxwindMph;
    final int maxwindKph;
    final double totalprecipMm;
    final double totalprecipIn;
    final int totalsnowCm;
    final int avgvisKm;
    final int avgvisMiles;
    final int avghumidity;
    final int dailyWillItRain;
    final int dailyChanceOfRain;
    final int dailyWillItSnow;
    final int dailyChanceOfSnow;
    final Condition condition;
    final int uv;

    Day({
        required this.maxtempC,
        required this.maxtempF,
        required this.mintempC,
        required this.mintempF,
        required this.avgtempC,
        required this.avgtempF,
        required this.maxwindMph,
        required this.maxwindKph,
        required this.totalprecipMm,
        required this.totalprecipIn,
        required this.totalsnowCm,
        required this.avgvisKm,
        required this.avgvisMiles,
        required this.avghumidity,
        required this.dailyWillItRain,
        required this.dailyChanceOfRain,
        required this.dailyWillItSnow,
        required this.dailyChanceOfSnow,
        required this.condition,
        required this.uv,
    });

    factory Day.fromRawJson(String str) => Day.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Day.fromJson(Map<String, dynamic> json) => Day(
        maxtempC: json["maxtemp_c"]?.toDouble(),
        maxtempF: json["maxtemp_f"]?.toDouble(),
        mintempC: json["mintemp_c"]?.toDouble(),
        mintempF: json["mintemp_f"]?.toDouble(),
        avgtempC: json["avgtemp_c"]?.toDouble(),
        avgtempF: json["avgtemp_f"]?.toDouble(),
        maxwindMph: json["maxwind_mph"]?.toDouble(),
        maxwindKph: json["maxwind_kph"],
        totalprecipMm: json["totalprecip_mm"]?.toDouble(),
        totalprecipIn: json["totalprecip_in"]?.toDouble(),
        totalsnowCm: json["totalsnow_cm"],
        avgvisKm: json["avgvis_km"],
        avgvisMiles: json["avgvis_miles"],
        avghumidity: json["avghumidity"],
        dailyWillItRain: json["daily_will_it_rain"],
        dailyChanceOfRain: json["daily_chance_of_rain"],
        dailyWillItSnow: json["daily_will_it_snow"],
        dailyChanceOfSnow: json["daily_chance_of_snow"],
        condition: Condition.fromJson(json["condition"]),
        uv: json["uv"],
    );

    Map<String, dynamic> toJson() => {
        "maxtemp_c": maxtempC,
        "maxtemp_f": maxtempF,
        "mintemp_c": mintempC,
        "mintemp_f": mintempF,
        "avgtemp_c": avgtempC,
        "avgtemp_f": avgtempF,
        "maxwind_mph": maxwindMph,
        "maxwind_kph": maxwindKph,
        "totalprecip_mm": totalprecipMm,
        "totalprecip_in": totalprecipIn,
        "totalsnow_cm": totalsnowCm,
        "avgvis_km": avgvisKm,
        "avgvis_miles": avgvisMiles,
        "avghumidity": avghumidity,
        "daily_will_it_rain": dailyWillItRain,
        "daily_chance_of_rain": dailyChanceOfRain,
        "daily_will_it_snow": dailyWillItSnow,
        "daily_chance_of_snow": dailyChanceOfSnow,
        "condition": condition.toJson(),
        "uv": uv,
    };
}

class Hour {
    final int timeEpoch;
    final String time;
    final double tempC;
    final double tempF;
    final int isDay;
    final Condition condition;
    final double windMph;
    final double windKph;
    final int windDegree;
    final WindDir windDir;
    final int pressureMb;
    final double pressureIn;
    final double precipMm;
    final int precipIn;
    final int humidity;
    final int cloud;
    final double feelslikeC;
    final double feelslikeF;
    final double windchillC;
    final double windchillF;
    final double heatindexC;
    final double heatindexF;
    final double dewpointC;
    final double dewpointF;
    final int willItRain;
    final int chanceOfRain;
    final int willItSnow;
    final int chanceOfSnow;
    final int visKm;
    final int visMiles;
    final double gustMph;
    final double gustKph;
    final int uv;

    Hour({
        required this.timeEpoch,
        required this.time,
        required this.tempC,
        required this.tempF,
        required this.isDay,
        required this.condition,
        required this.windMph,
        required this.windKph,
        required this.windDegree,
        required this.windDir,
        required this.pressureMb,
        required this.pressureIn,
        required this.precipMm,
        required this.precipIn,
        required this.humidity,
        required this.cloud,
        required this.feelslikeC,
        required this.feelslikeF,
        required this.windchillC,
        required this.windchillF,
        required this.heatindexC,
        required this.heatindexF,
        required this.dewpointC,
        required this.dewpointF,
        required this.willItRain,
        required this.chanceOfRain,
        required this.willItSnow,
        required this.chanceOfSnow,
        required this.visKm,
        required this.visMiles,
        required this.gustMph,
        required this.gustKph,
        required this.uv,
    });

    factory Hour.fromRawJson(String str) => Hour.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Hour.fromJson(Map<String, dynamic> json) => Hour(
        timeEpoch: json["time_epoch"],
        time: json["time"],
        tempC: json["temp_c"]?.toDouble(),
        tempF: json["temp_f"]?.toDouble(),
        isDay: json["is_day"],
        condition: Condition.fromJson(json["condition"]),
        windMph: json["wind_mph"]?.toDouble(),
        windKph: json["wind_kph"]?.toDouble(),
        windDegree: json["wind_degree"],
        windDir: windDirValues.map[json["wind_dir"]]!,
        pressureMb: json["pressure_mb"],
        pressureIn: json["pressure_in"]?.toDouble(),
        precipMm: json["precip_mm"]?.toDouble(),
        precipIn: json["precip_in"],
        humidity: json["humidity"],
        cloud: json["cloud"],
        feelslikeC: json["feelslike_c"]?.toDouble(),
        feelslikeF: json["feelslike_f"]?.toDouble(),
        windchillC: json["windchill_c"]?.toDouble(),
        windchillF: json["windchill_f"]?.toDouble(),
        heatindexC: json["heatindex_c"]?.toDouble(),
        heatindexF: json["heatindex_f"]?.toDouble(),
        dewpointC: json["dewpoint_c"]?.toDouble(),
        dewpointF: json["dewpoint_f"]?.toDouble(),
        willItRain: json["will_it_rain"],
        chanceOfRain: json["chance_of_rain"],
        willItSnow: json["will_it_snow"],
        chanceOfSnow: json["chance_of_snow"],
        visKm: json["vis_km"],
        visMiles: json["vis_miles"],
        gustMph: json["gust_mph"]?.toDouble(),
        gustKph: json["gust_kph"]?.toDouble(),
        uv: json["uv"],
    );

    Map<String, dynamic> toJson() => {
        "time_epoch": timeEpoch,
        "time": time,
        "temp_c": tempC,
        "temp_f": tempF,
        "is_day": isDay,
        "condition": condition.toJson(),
        "wind_mph": windMph,
        "wind_kph": windKph,
        "wind_degree": windDegree,
        "wind_dir": windDirValues.reverse[windDir],
        "pressure_mb": pressureMb,
        "pressure_in": pressureIn,
        "precip_mm": precipMm,
        "precip_in": precipIn,
        "humidity": humidity,
        "cloud": cloud,
        "feelslike_c": feelslikeC,
        "feelslike_f": feelslikeF,
        "windchill_c": windchillC,
        "windchill_f": windchillF,
        "heatindex_c": heatindexC,
        "heatindex_f": heatindexF,
        "dewpoint_c": dewpointC,
        "dewpoint_f": dewpointF,
        "will_it_rain": willItRain,
        "chance_of_rain": chanceOfRain,
        "will_it_snow": willItSnow,
        "chance_of_snow": chanceOfSnow,
        "vis_km": visKm,
        "vis_miles": visMiles,
        "gust_mph": gustMph,
        "gust_kph": gustKph,
        "uv": uv,
    };
}

class Location {
    final String name;
    final String region;
    final String country;
    final double lat;
    final double lon;
    final String tzId;
    final int localtimeEpoch;
    final String localtime;

    Location({
        required this.name,
        required this.region,
        required this.country,
        required this.lat,
        required this.lon,
        required this.tzId,
        required this.localtimeEpoch,
        required this.localtime,
    });

    factory Location.fromRawJson(String str) => Location.fromJson(json.decode(str));

    String toRawJson() => json.encode(toJson());

    factory Location.fromJson(Map<String, dynamic> json) => Location(
        name: json["name"],
        region: json["region"],
        country: json["country"],
        lat: json["lat"]?.toDouble(),
        lon: json["lon"]?.toDouble(),
        tzId: json["tz_id"],
        localtimeEpoch: json["localtime_epoch"],
        localtime: json["localtime"],
    );

    Map<String, dynamic> toJson() => {
        "name": name,
        "region": region,
        "country": country,
        "lat": lat,
        "lon": lon,
        "tz_id": tzId,
        "localtime_epoch": localtimeEpoch,
        "localtime": localtime,
    };
}

class EnumValues<T> {
    Map<String, T> map;
    late Map<T, String> reverseMap;

    EnumValues(this.map);

    Map<T, String> get reverse {
        reverseMap = map.map((k, v) => MapEntry(v, k));
        return reverseMap;
    }
}'''
© www.soinside.com 2019 - 2024. All rights reserved.