skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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’t assign that to the double here

    you can probably just directly do

      temperature = weatherData['main']['temp'];
    
    Login or Signup to reply.
  3. Can you try making temp as dynamic

    dynamic temperature;
    
    Login or Signup to reply.
  4. Dont use .toInt()

    Use

    temperature = int.parse(temp);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search