-1

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

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

══╡ 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

主要.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的建议改了代码,但是终端没有问题,但是模拟器出现了错误。

我按照 Visual Studio 的建议更改了代码

模拟器发生错误

4

1 回答 1

0

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

于 2021-07-07T04:19:36.620 回答