
[英]Flutter - Exception: type 'int' is not a subtype of type 'double'
[英]type 'int' is not a subtype of 'double' flutter even after using toInt()
在我的应用程序中,我将 temp.toInt() 的双温度转换为后期 int 温度变量。 但不知何故,我的应用程序崩溃并向我显示错误消息“type 'int' is not a subtype of 'double'”。 主要问题是它突然起作用。 然后它再次崩溃。 我不知道为什么会这样。 这是我的代码-
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'];
});
}
我应该怎么办? 如果您有任何建议,请告诉我。 提前致谢。
我试过声明另一个 int 变量并将其分配给 temperature 但这也不起作用。
看到代码和错误似乎错误实际上必须在这一行:
double temp = weatherData['main']['temp'];
这意味着它已经是一个 int 并且你不能在这里将它分配给 double
你可能可以直接做
temperature = weatherData['main']['temp'];
你能试着把温度设为动态的吗
dynamic temperature;
通过将 weatherData['main']['temp'] 转换为双精度值 - weatherData['main']['temp'].toDouble(); 解决了问题。 同样通过将温度声明为动态动态温度也解决了这个问题。
不要使用.toInt()
利用
temperature = int.parse(temp);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.