“Null”类型不是“int”类型的子类型 main.dart模型.dartweather_screen.dart

问题描述

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

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

══╡ 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(),);
  }
}

模型.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 中的第 13 行进行检查。 发生这种情况是因为您将空值分配给使用 int 类型定义的变量。 如我所见,您的条件和索引被定义为 int。仔细检查一下,你会没事的