I am developing a weather app in flutter and encountered an issue where when we type in the city name in the search bar and click on the next icon it displays the weather of that particular place on the screen. But just to make sure that the data is being parsed properly, I'm only calling the data in the console. Though the code shows no errors, multiple errors are being thrown in the console. Could anyone please tell me how I can solve this error and parse this data successfully?
The error displayed in the console is given below:
An Observatory debugger and profiler on Chrome is available at: http://127.0.0.1:50548/vYt04KYoAA8=
The Flutter DevTools debugger and profiler on Chrome is available at:
http://127.0.0.1:9101?uri=http://127.0.0.1:50548/vYt04KYoAA8=
{"cod":"400","message":"Nothing to geocode"}
Error: Exception: Failed to get posts
at Object.throw_ [as throw] (http://localhost:50501/dart_sdk.js:5061:11)
at getWeather (http://localhost:50501/packages/finalproject/Weatherproj/data_service.dart.lib.js:31:21)
at getWeather.next (<anonymous>)
at http://localhost:50501/dart_sdk.js:38640:33
at _RootZone.runUnary (http://localhost:50501/dart_sdk.js:38511:59)
at _FutureListener.thenAwait.handleValue (http://localhost:50501/dart_sdk.js:33713:29)
at handleValueCallback (http://localhost:50501/dart_sdk.js:34265:49)
at Function._propagateToListeners (http://localhost:50501/dart_sdk.js:34303:17)
at _Future.new.[_completeWithValue] (http://localhost:50501/dart_sdk.js:34151:23)
at async._AsyncCallbackEntry.new.callback (http://localhost:50501/dart_sdk.js:34172:35)
at Object._microtaskLoop (http://localhost:50501/dart_sdk.js:38778:13)
at _startMicrotaskLoop (http://localhost:50501/dart_sdk.js:38784:13)
at http://localhost:50501/dart_sdk.js:34519:9
Below is the code for the model class:
class WeatherAppDetails {
City? city;
String? cod;
double? message;
int? cnt;
List<ListDetails>? list;
WeatherAppDetails(
{required this.city,
required this.cod,
required this.message,
required this.cnt,
required this.list});
WeatherAppDetails.fromJson(Map<String, dynamic> json) {
final cod = json['cod'];
final message = json['message'];
final cnt = json['cnt'];
if (json['list'] != null) {
list = <ListDetails>[];
json['list'].forEach((v) {
list!.add(ListDetails.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (city != null) {
data['city'] = city!.toJson();
}
data['cod'] = cod;
data['message'] = message;
data['cnt'] = cnt;
if (list != null) {
data['list'] = list!.map((v) => v.toJson()).toList();
}
return data;
}
}
class City {
int? id;
String? name;
Coord? coord;
String? country;
int? population;
int? timezone;
City(
{required this.id,
required this.name,
required this.coord,
required this.country,
required this.population,
required this.timezone});
City.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
coord = (json['coord'] != null ? Coord.fromJson(json['coord']) : null)!;
country = json['country'];
population = json['population'];
timezone = json['timezone'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['name'] = name;
if (coord != null) {
data['coord'] = coord!.toJson();
}
data['country'] = country;
data['population'] = population;
data['timezone'] = timezone;
return data;
}
}
class Coord {
double? lon;
double? lat;
Coord({required this.lon, required this.lat});
Coord.fromJson(Map<String, dynamic> json) {
lon = json['lon'];
lat = json['lat'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['lon'] = lon;
data['lat'] = lat;
return data;
}
}
class ListDetails {
int? dt;
int? sunrise;
int? sunset;
Temp? temp;
FeelsLike? feelsLike;
int? pressure;
int? humidity;
List<WeatherDetails>? weather;
double? speed;
int? deg;
double? gust;
int? clouds;
double? pop;
double? rain;
ListDetails(
{required this.dt,
required this.sunrise,
required this.sunset,
required this.temp,
required this.feelsLike,
required this.pressure,
required this.humidity,
required this.weather,
required this.speed,
required this.deg,
required this.gust,
required this.clouds,
required this.pop,
required this.rain});
ListDetails.fromJson(Map<String, dynamic> json) {
dt = json['dt'];
sunrise = json['sunrise'];
sunset = json['sunset'];
temp = json['temp'] != null ? Temp.fromJson(json['temp']) : null;
feelsLike = json['feels_like'] != null
? FeelsLike.fromJson(json['feels_like'])
: null;
pressure = json['pressure'];
humidity = json['humidity'];
if (json['weather'] != null) {
weather = <WeatherDetails>[];
json['weather'].forEach((v) {
weather!.add(WeatherDetails.fromJson(v));
});
}
speed = json['speed'];
deg = json['deg'];
gust = json['gust'];
clouds = json['clouds'];
pop = json['pop'];
rain = json['rain'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['dt'] = dt;
data['sunrise'] = sunrise;
data['sunset'] = sunset;
if (temp != null) {
data['temp'] = temp!.toJson();
}
if (feelsLike != null) {
data['feels_like'] = feelsLike!.toJson();
}
data['pressure'] = pressure;
data['humidity'] = humidity;
if (weather != null) {
data['weather'] = weather!.map((v) => v.toJson()).toList();
}
data['speed'] = speed;
data['deg'] = deg;
data['gust'] = gust;
data['clouds'] = clouds;
data['pop'] = pop;
data['rain'] = rain;
return data;
}
}
class Temp {
double? day;
double? min;
double? max;
double? night;
double? eve;
double? morn;
Temp(
{required this.day,
required this.min,
required this.max,
required this.night,
required this.eve,
required this.morn});
Temp.fromJson(Map<String, dynamic> json) {
day = json['day'];
min = json['min'];
max = json['max'];
night = json['night'];
eve = json['eve'];
morn = json['morn'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['day'] = day;
data['min'] = min;
data['max'] = max;
data['night'] = night;
data['eve'] = eve;
data['morn'] = morn;
return data;
}
}
class FeelsLike {
double? day;
double? night;
double? eve;
double? morn;
FeelsLike(
{required this.day,
required this.night,
required this.eve,
required this.morn});
FeelsLike.fromJson(Map<String, dynamic> json) {
day = json['day'];
night = json['night'];
eve = json['eve'];
morn = json['morn'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['day'] = day;
data['night'] = night;
data['eve'] = eve;
data['morn'] = morn;
return data;
}
}
class WeatherDetails {
int? id;
String? main;
String? description;
String? icon;
WeatherDetails(
{required this.id,
required this.main,
required this.description,
required this.icon});
WeatherDetails.fromJson(Map<String, dynamic> json) {
id = json['id'];
main = json['main'];
description = json['description'];
icon = json['icon'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = id;
data['main'] = main;
data['description'] = description;
data['icon'] = icon;
return data;
}
}
Below is the code for the network class:
class DataService {
Future<WeatherAppDetails> getWeather(String name) async {
final queryParameters = {
'q': name,
'appid': '3c044b7295d2df14f8bee74c19d4b96f',
'units': 'imperial',
'cnt': '7'
};
final uri = Uri.https(
'api.openweathermap.org', '/data/2.5/forecast/daily', queryParameters);
final response = await http.get(uri);
print(response.body);
if (response.statusCode == 200) {
return WeatherAppDetails.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to get posts');
}
}
}