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.
CodePudding user response:
Can you try making temp as dynamic
dynamic temperature;
CodePudding user response:
Seeing the code and the error it seems the error must actually be on this line:
double temp = weatherData['main']['temp'];
Meaning that it already is an int and you can's assign that to the double here
you can probably just directly do
temperature = weatherData['main']['temp'];
CodePudding user response:
by converting weatherData['main']['temp'] to a double value - weatherData['main']['temp'].toDouble(); solves the problem. also by declaring temperature as dynamic dynamic temperature also solves the problem.
CodePudding user response:
Dont use .toInt()
Use
temperature = int.parse(temp);