【发布时间】:2023-01-24 20:14:58
【问题描述】:
在我的应用程序中,我将 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 但这也不起作用。
【问题讨论】:
-
你能试试 print(weatherData['main']['temp']) 在将它分配给
temp之前看看它的类型吗
标签: flutter dart android-emulator typeerror subtype