类型 'Null' 不是类型 'int' 的子类型
type 'Null' is not a subtype of type 'int'
我在研究颤振。我最后遇到了这个错误,我不知道该怎么做。如果你能告诉我怎么做,我将不胜感激。对不起,如果我的英语不够好,请原谅我。
我在终端中也遇到了这个错误。
它是文件的一部分。
══╡ 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 中的第 13 行开始检查。
发生这种情况是因为您将 null 值分配给了用 int 类型定义的变量。
如我所见,您的条件和索引都定义为 int。仔细检查,你会没事的
我在研究颤振。我最后遇到了这个错误,我不知道该怎么做。如果你能告诉我怎么做,我将不胜感激。对不起,如果我的英语不够好,请原谅我。
我在终端中也遇到了这个错误。 它是文件的一部分。
══╡ 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 中的第 13 行开始检查。 发生这种情况是因为您将 null 值分配给了用 int 类型定义的变量。 如我所见,您的条件和索引都定义为 int。仔细检查,你会没事的