【问题标题】:type 'int' is not a subtype of 'double' flutter even after using toInt()即使在使用 toInt() 之后,类型 \'int\' 也不是 \'double\' 的子类型
【发布时间】: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


【解决方案1】:

看到代码和错误似乎错误实际上必须在这一行:

  double temp = weatherData['main']['temp'];

这意味着它已经是一个 int 并且您可以在此处将其分配给 double

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 2022-10-13
    • 2021-08-12
    • 2020-09-29
    • 2021-05-16
    • 2020-08-07
    • 2022-11-24
    • 1970-01-01
    • 2020-10-30
    相关资源
    最近更新 更多