In my app I’m converting a double temp by temp.toInt() to a late int temperature variable. But somehow my app crashes and showing me error saying "type ‘int’ is not a subtype of ‘double’". The main problem is it works suddenly. And then again it crashes. I don’t know why it’s happening. here is my code-
class _LocationScreenState extends State<LocationScreen> {
WeatherModel weather = WeatherModel();
late int temperature;
late String cityName;
late String weatherIcon;
late String weatherMessage;
@override
void initState() {
super.initState();
updateUI(widget.locationWeather);
}
void updateUI(dynamic weatherData) {
setState(() {
if (weatherData == null) {
temperature = 0;
weatherIcon = 'Error';
weatherMessage = 'Unable to get weather data';
cityName = '';
return;
}
double temp = weatherData['main']['temp'];
temperature = temp.toInt();
var condition = weatherData['weather'][0]['id'];
weatherIcon = weather.getWeatherIcon(condition);
weatherMessage = weather.getMessage(temperature);
cityName = weatherData['name'];
});
}
what should I do? please let me know if you have any advice.
Thanks in advance.
I’ve tried declaring another int variable and assign it to temperature but that didn’t work either.
4
Answers
The value is sometimes an int, sometimes a double. By converting weatherData['main']['temp'] to a double value - weatherData['main']['temp'].toDouble(); solves the problem.
Seeing the code and the error it seems the error must actually be on this line:
Meaning that it already is an int and you can’t assign that to the double here
you can probably just directly do
Can you try making temp as dynamic
Dont use .toInt()
Use