Home > Mobile >  i keep getting type 'int' is not a subtype of type 'double'
i keep getting type 'int' is not a subtype of type 'double'

Time:01-04

I'm trying to convert a double to an integer

I even tried storing temperature as a Var but I kept getting the same error

CodePudding user response:

Your temperature is defined as double. Just do temperature = temp; or change the type of temperature to int

CodePudding user response:

You have to use a toStringAsFixed() function combined with int.parse() function in order to convert your double to int.

double temp = weatherData['main']['temp'];
temperature = int.parse(temp.toStringAsFixed(0));

CodePudding user response:

You can use num to support both int and double if you don't know what type you will receive. It will determine the type in runtime. Then you can call .toInt() or .toDouble() functions.

  • Related