0

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');
    }
  }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
Rajesh
  • 91
  • 8
  • What do you set in `city`? Seems there is a error on the request. Can you share your complete request? – Maikzen Nov 11 '21 at 11:35
  • Hi! The error that I previously mentioned was wrong. I had edited that part. Could you please go through that once? – Rajesh Nov 11 '21 at 11:43
  • I had shared the model file in the above question where the city is nothing but the name of the city which is a string. – Rajesh Nov 11 '21 at 11:48
  • Yeah but i mean what literal String you pass to request, because the error is in that city, api doesnt recognize it well – Maikzen Nov 11 '21 at 11:54
  • I changed the city to name in the network class file and the else part of the code is getting executed along with a few errors. – Rajesh Nov 11 '21 at 11:57
  • I dont mean that, i mean when you call to api wheather, what value appear on `q`? `data/2.5/forecast/daily?appid=3c044b7295d2df14f8bee74c19d4b96f&q=HERE` – Maikzen Nov 11 '21 at 12:01
  • The city name that we type in the search bar – Rajesh Nov 11 '21 at 12:15
  • So the problem is that city name is incorrect and api doesnt find it – Maikzen Nov 11 '21 at 12:17
  • How do I go about it? – Rajesh Nov 11 '21 at 16:06

1 Answers1

0

Basically api.weather is telling you that the given value of "q" param does not exists, no geolocation found.

It´s helpful to add city param input

Your city input on the request is wrong, this should work (example)

https://api.openweathermap.org/data/2.5/forecast/daily?appid=3c044b7295d2df14f8bee74c19d4b96f&q=london,uk

yaguarete
  • 640
  • 2
  • 5
  • 13
  • Could you specify how I can make changes in the above code to get the output? I'm new to flutter so I'm not sure how to go about it. And thanks a ton for the input. – Rajesh Nov 11 '21 at 16:06
  • Hi @rajesh, can you print and add it to the post the variable "name" you are using on the function getWeather() – yaguarete Nov 11 '21 at 16:10
  • I'm sorry, I didn't get you. Can you be a little more specific? – Rajesh Nov 11 '21 at 17:08
  • Your parameter when doing the request seems wrong, as stated before. Can you add to your post all request parameters? 'q': name, <-- name parameter... can you show the string you are sending on the request? – yaguarete Nov 12 '21 at 07:40