I’m a beginner in Flutter and I’m facing an issue while working on a weather application, which I’m using as a learning project. When I make a call to the OpenWeatherMap API, I encounter an error when trying to parse the JSON data. The error message I receive is:
"Unhandled Exception: type ‘Null’ is not a subtype of type ‘String’."
I’m seeking assistance in resolving this problem. Below is the specific part of the code that’s causing the issue:
api() async{
late String? lat = null;
late String? long = null;
if ((latAddressSelected != null) && (longAddressSelected != null)){
lat = latAddressSelected;
long = longAddressSelected;
} else if (locationData.longitude != null && locationData.latitude != null){
latAddressSelected = locationData.latitude.toString();
longAddressSelected = locationData.longitude.toString();
}
if ((lat != null) && (long != null)){
const key = "&appid=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
String lang = "&lang=${Localizations.localeOf(context).languageCode}";
String baseAPI = "http://api.openweathermap.org/data/2.5/forecast?";
String coordString = "lat=$latAddressSelected&lon=$longAddressSelected";
String units = "&units=metrics";
String totalString = baseAPI+coordString+lang+key+units;
final response = await http.get(Uri.parse(totalString));
if (response.statusCode == 200){
Temperature temps = new Temperature();
Map map = json.decode(response.body);
temps.fromJSON(map); //<==================error here
print(map);
setState(() {
temperature = temps;
});
}
}
}
My Temperature class
class Temperature {
late String name;
late String main;
late String description;
late String icon;
var temp;
var pressure;
var humidity;
var temp_min;
var temp_max;
Temperature();
void fromJSON(Map map) {
this.name = map["name"];
List weather = map["weather"];
Map mapWeather = weather[0];
this.main = mapWeather["main"];
this.description = mapWeather["description"];
Map main = map["main"];
this.temp = main["temp"];
this.pressure = main["pressure"];
this.humidity = main["humidity"];
this.temp_min = main["temp_min"];
this.temp_max = main["temp_max"];
}
}
The response from API:
{cod: 200, message: 0, cnt: 40, list: [{dt: 1698440400, main: {temp: 285.12, feels_like: 284.51, temp_min: 285.12, temp_max: 285.17, pressure: 1007, sea_level: 1007, grnd_level: 990, humidity: 82, temp_kf: -0.05}, weather: [{id: 800, main: Clear, description: clear sky, icon: 01n}], clouds: {all: 0}, wind: {speed: 2, deg: 208, gust: 2.38}, visibility: 10000, pop: 0.02, sys: {pod: n}, dt_txt: 2023-10-27 21:00:00}, {dt: 1698451200, main: {temp: 284.91, feels_like: 284.26, temp_min: 284.49, temp_max: 284.91, pressure: 1007, sea_level: 1007, grnd_level: 990, humidity: 81, temp_kf: 0.42}, weather: [{id: 801, main: Clouds, description: few clouds, icon: 02n}], clouds: {all: 18}, wind: {speed: 1.9, deg: 209, gust: 2.13}, visibility: 10000, pop: 0, sys: {pod: n}, dt_txt: 2023-10-28 00:00:00}, {dt: 1698462000, main: {temp: 284.17, feels_like: 283.49, temp_min: 283.69, temp_max: 284.17, pressure: 1006, sea_level: 1006, grnd_level: 988, humidity: 83, temp_kf: 0.48}, weather: [{id: 802, main: Clouds, description: scatter
My error when I call ‘map’:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'String'
E/flutter (14939): #0 Temperature.fromJSON (package:app_meteo/Temperature.dart:17:10)
E/flutter (14939): #1 _MyHomePageState.api (package:app_meteo/main.dart:263:15)
E/flutter (14939): <asynchronous suspension>
I’ve already tried to use the code from my course and see many tutorial, but none of them have worked. Any guidance or suggestions would be greatly appreciated.
2
Answers
It means that you are trying to read a key that either doesn’t exist or has a null value.
At your error log it says:
Since we can’t see your IDE, you need to check on your editor at line 17 what key are you trying to read.
You can check if the key you are trying to read is there, by printing the map before acessing it.
For example:
By doing so at the lines on the error log, you should be able to get a hint on what you are missing.
Often, it is not that the key doesn’t exists, but that it is inside of another map to reach it.
try to make all variable nullable like this
or to handle what value is null in from json function
in every assign value use ?? to set your message to view which value return null value after you make the variable nullable like this