|
| 1 | +// To parse this JSON data, do |
| 2 | +// |
| 3 | +// final weather = weatherFromJson(jsonString); |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | + |
| 7 | +Weather weatherFromJson(String str) => Weather.fromJson(json.decode(str)); |
| 8 | + |
| 9 | +String weatherToJson(Weather data) => json.encode(data.toJson()); |
| 10 | + |
| 11 | +class Weather { |
| 12 | + Weather({ |
| 13 | + this.location, |
| 14 | + this.current, |
| 15 | + }); |
| 16 | + |
| 17 | + Location location; |
| 18 | + Current current; |
| 19 | + |
| 20 | + factory Weather.fromJson(Map<String, dynamic> json) => Weather( |
| 21 | + location: Location.fromJson(json["location"]), |
| 22 | + current: Current.fromJson(json["current"]), |
| 23 | + ); |
| 24 | + |
| 25 | + Map<String, dynamic> toJson() => { |
| 26 | + "location": location.toJson(), |
| 27 | + "current": current.toJson(), |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +class Current { |
| 32 | + Current({ |
| 33 | + this.lastUpdatedEpoch, |
| 34 | + this.lastUpdated, |
| 35 | + this.tempC, |
| 36 | + this.tempF, |
| 37 | + this.isDay, |
| 38 | + this.condition, |
| 39 | + this.windMph, |
| 40 | + this.windKph, |
| 41 | + this.windDegree, |
| 42 | + this.windDir, |
| 43 | + this.pressureMb, |
| 44 | + this.pressureIn, |
| 45 | + this.precipMm, |
| 46 | + this.precipIn, |
| 47 | + this.humidity, |
| 48 | + this.cloud, |
| 49 | + this.feelslikeC, |
| 50 | + this.feelslikeF, |
| 51 | + this.visKm, |
| 52 | + this.visMiles, |
| 53 | + this.uv, |
| 54 | + this.gustMph, |
| 55 | + this.gustKph, |
| 56 | + this.airQuality, |
| 57 | + }); |
| 58 | + |
| 59 | + int lastUpdatedEpoch; |
| 60 | + String lastUpdated; |
| 61 | + double tempC; |
| 62 | + double tempF; |
| 63 | + int isDay; |
| 64 | + Condition condition; |
| 65 | + double windMph; |
| 66 | + double windKph; |
| 67 | + int windDegree; |
| 68 | + String windDir; |
| 69 | + double pressureMb; |
| 70 | + double pressureIn; |
| 71 | + double precipMm; |
| 72 | + double precipIn; |
| 73 | + int humidity; |
| 74 | + int cloud; |
| 75 | + double feelslikeC; |
| 76 | + double feelslikeF; |
| 77 | + double visKm; |
| 78 | + double visMiles; |
| 79 | + double uv; |
| 80 | + double gustMph; |
| 81 | + double gustKph; |
| 82 | + Map<String, double> airQuality; |
| 83 | + |
| 84 | + factory Current.fromJson(Map<String, dynamic> json) => Current( |
| 85 | + lastUpdatedEpoch: json["last_updated_epoch"], |
| 86 | + lastUpdated: json["last_updated"], |
| 87 | + tempC: json["temp_c"], |
| 88 | + tempF: json["temp_f"], |
| 89 | + isDay: json["is_day"], |
| 90 | + condition: Condition.fromJson(json["condition"]), |
| 91 | + windMph: json["wind_mph"].toDouble(), |
| 92 | + windKph: json["wind_kph"].toDouble(), |
| 93 | + windDegree: json["wind_degree"], |
| 94 | + windDir: json["wind_dir"], |
| 95 | + pressureMb: json["pressure_mb"], |
| 96 | + pressureIn: json["pressure_in"].toDouble(), |
| 97 | + precipMm: json["precip_mm"].toDouble(), |
| 98 | + precipIn: json["precip_in"], |
| 99 | + humidity: json["humidity"], |
| 100 | + cloud: json["cloud"], |
| 101 | + feelslikeC: json["feelslike_c"].toDouble(), |
| 102 | + feelslikeF: json["feelslike_f"].toDouble(), |
| 103 | + visKm: json["vis_km"], |
| 104 | + visMiles: json["vis_miles"], |
| 105 | + uv: json["uv"], |
| 106 | + gustMph: json["gust_mph"].toDouble(), |
| 107 | + gustKph: json["gust_kph"].toDouble(), |
| 108 | + ); |
| 109 | + |
| 110 | + Map<String, dynamic> toJson() => { |
| 111 | + "last_updated_epoch": lastUpdatedEpoch, |
| 112 | + "last_updated": lastUpdated, |
| 113 | + "temp_c": tempC, |
| 114 | + "temp_f": tempF, |
| 115 | + "is_day": isDay, |
| 116 | + "condition": condition.toJson(), |
| 117 | + "wind_mph": windMph, |
| 118 | + "wind_kph": windKph, |
| 119 | + "wind_degree": windDegree, |
| 120 | + "wind_dir": windDir, |
| 121 | + "pressure_mb": pressureMb, |
| 122 | + "pressure_in": pressureIn, |
| 123 | + "precip_mm": precipMm, |
| 124 | + "precip_in": precipIn, |
| 125 | + "humidity": humidity, |
| 126 | + "cloud": cloud, |
| 127 | + "feelslike_c": feelslikeC, |
| 128 | + "feelslike_f": feelslikeF, |
| 129 | + "vis_km": visKm, |
| 130 | + "vis_miles": visMiles, |
| 131 | + "uv": uv, |
| 132 | + "gust_mph": gustMph, |
| 133 | + "gust_kph": gustKph, |
| 134 | + "air_quality": Map.from(airQuality).map((k, v) => MapEntry<String, dynamic>(k, v)), |
| 135 | + }; |
| 136 | +} |
| 137 | + |
| 138 | +class Condition { |
| 139 | + Condition({ |
| 140 | + this.text, |
| 141 | + this.icon, |
| 142 | + this.code, |
| 143 | + }); |
| 144 | + |
| 145 | + String text; |
| 146 | + String icon; |
| 147 | + int code; |
| 148 | + |
| 149 | + factory Condition.fromJson(Map<String, dynamic> json) => Condition( |
| 150 | + text: json["text"], |
| 151 | + icon: json["icon"], |
| 152 | + code: json["code"], |
| 153 | + ); |
| 154 | + |
| 155 | + Map<String, dynamic> toJson() => { |
| 156 | + "text": text, |
| 157 | + "icon": icon, |
| 158 | + "code": code, |
| 159 | + }; |
| 160 | +} |
| 161 | + |
| 162 | +class Location { |
| 163 | + Location({ |
| 164 | + this.name, |
| 165 | + this.region, |
| 166 | + this.country, |
| 167 | + this.lat, |
| 168 | + this.lon, |
| 169 | + this.tzId, |
| 170 | + this.localtimeEpoch, |
| 171 | + this.localtime, |
| 172 | + }); |
| 173 | + |
| 174 | + String name; |
| 175 | + String region; |
| 176 | + String country; |
| 177 | + double lat; |
| 178 | + double lon; |
| 179 | + String tzId; |
| 180 | + int localtimeEpoch; |
| 181 | + String localtime; |
| 182 | + |
| 183 | + factory Location.fromJson(Map<String, dynamic> json) => Location( |
| 184 | + name: json["name"], |
| 185 | + region: json["region"], |
| 186 | + country: json["country"], |
| 187 | + lat: json["lat"].toDouble(), |
| 188 | + lon: json["lon"].toDouble(), |
| 189 | + tzId: json["tz_id"], |
| 190 | + localtimeEpoch: json["localtime_epoch"], |
| 191 | + localtime: json["localtime"], |
| 192 | + ); |
| 193 | + |
| 194 | + Map<String, dynamic> toJson() => { |
| 195 | + "name": name, |
| 196 | + "region": region, |
| 197 | + "country": country, |
| 198 | + "lat": lat, |
| 199 | + "lon": lon, |
| 200 | + "tz_id": tzId, |
| 201 | + "localtime_epoch": localtimeEpoch, |
| 202 | + "localtime": localtime, |
| 203 | + }; |
| 204 | +} |
0 commit comments