Search code examples
flutterdartnull-safety

type 'Null' is not a subtype of type 'int'


I was studying flutter. I got this error at the end and I'm not sure what to do. I would appreciate it if you could tell me how. I'm sorry if my English is not good enough, please forgive me.

I also got this error in the terminal. It is part of the file.

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

I changed the code as recommended by visual studio, but there was no problem in the terminal, but an error occurred in the simulator.

I changed the code as recommended by visual studio

error occurred in the simulator


Solution

  • Just check from the line 13 in your main.dart as the error said. It happened because you assign the null value to the variable which was defined with int type. As I see , you have condition and index are defined as int. Check it carefully and you will be fine