【问题标题】:type 'Null' is not a subtype of type 'int'“Null”类型不是“int”类型的子类型
【发布时间】:2021-07-07 03:50:36
【问题描述】:

我正在研究颤振。我最后得到了这个错误,我不知道该怎么做。如果你能告诉我怎么做,我将不胜感激。如果我的英语不够好,请见谅。

我在终端中也遇到了这个错误。 它是文件的一部分。

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following _TypeError was thrown building Builder:
type 'Null' is not a subtype of type 'int'

The relevant error-causing widget was:
  MaterialApp
  file:///Users/lsw/Desktop/Flutter/FlutterPractice/Weather_ap/weather_app/lib/main.dart:13:12

main.dart

import 'package:flutter/material.dart';
import 'package:weather_app/screens/loading.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "Weather app",
      theme: ThemeData(primarySwatch: Colors.blue),
      home: Loading(),
    );
  }
}

model.dart

class Model {
  Widget enter image description heregetWeatherIcon(int condition) {
    if (condition < 300) {
      return SvgPicture.asset('assets/svg/climacon-colud_lightning.svg',
          color: Colors.black87);
    } else if (condition < 600) {
      return SvgPicture.asset('assets/svg/climacon-colud_snow_alt.svg',
          color: Colors.black87);
    } else if (condition == 800) {
      return SvgPicture.asset('assets/svg/climacon-sun.svg',
          color: Colors.black87);
    } else if (condition <= 804) {
      return SvgPicture.asset('assets/svg/climacon-cloud_sun.svg',
          color: Colors.black87);
    }
  }

 .
 .
 .

weather_screen.dart

 void updateData(dynamic weatherData, dynamic airData) {
    double temp2 = weatherData['main']['temp'];
    int condition = weatherData['weather'][0]['id'];
    int index = airData['list'][0]['main']['api'];
    des = weatherData['weather'][0]['description'];
    dust1 = airData['list'][0]['components']['pm10'];
    dust2 = airData['list'][0]['components']['pm2_5'];
    temp = temp2.round();
    cityName = weatherData['name'];
    icon = model.getWeatherIcon(condition) as Widget;
    airIcon = model.getAirIcon(index) as Widget;
    airState = model.getAirCondition(index) as Widget;
    print('온도: $temp');
    print('도시: $cityName');
  }
 

我按照visual studio的建议改了代码,但是终端没有问题,但是模拟器出错了。

I changed the code as recommended by visual studio

error occurred in the simulator

【问题讨论】:

  • 你能添加你的`main.dart`代码吗?
  • 添加了请求的 main.dart。谢谢。

标签: flutter dart null-safety


【解决方案1】:

如错误所述,只需从 main.dart 中的第 13 行检查即可。 发生这种情况是因为您将 null 值分配给使用 int 类型定义的变量。 如我所见,您将条件和索引定义为 int。仔细检查,你会没事的

【讨论】:

  • 我已经添加了main.dart,但我还是不太擅长,所以我不知道如何修复它。
  • 我认为问题是 weather_screen.dart 中的变量之一。只需注释所有变量,打开每个变量并运行以查看它返回的内容。你会发现哪个变量是错误的类型
猜你喜欢
  • 1970-01-01
  • 2020-12-14
  • 2020-03-18
  • 1970-01-01
  • 2021-10-16
  • 2021-10-09
  • 1970-01-01
  • 2020-12-25
  • 2021-07-15
相关资源
最近更新 更多