[Solved] type 'int' is not a subtype of 'double' flutter even after using toInt()

Question

Asked by Ib P on January 24, 2023 (source).

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.

Answer

Question answered by AlishanMuhammadAmin (source).

Can you try making temp as dynamic

dynamic temperature;
ANDROID-EMULATOR DART FLUTTER SUBTYPE TYPEERROR
SHARE: