0

I got this error when I run my simple flutter APP. I could not figure out why this error occurred.

Error

Null check operator used on a null value

import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:star_book/src/controller/mapDataController.dart';

import 'package:star_book/src/model/app_settings.dart';
import 'package:star_book/src/model/category.dart';
import 'package:star_book/src/model/map_data.dart';

class MapInformationPage extends StatefulWidget {
  late final String selectedMap;

  MapInformationPage(this.selectedMap);

  @override
  _MapInformationPageState createState() => _MapInformationPageState();
}

class _MapInformationPageState extends State<MapInformationPage> {
  final _mapController = Get.put(MapDataController());
  late final MapData mapData;
  late AppSettings _appSettings;
  late List<Category> _favoriteIcons;
  late int _favoriteIconsIndex;

  @override
  void initState() {
    super.initState();
    _appSettings = AppSettings();
    _favoriteIcons = [
      Category(Icons.favorite_border, Colors.black),
      Category(Icons.favorite, Colors.red),
    ];
    _favoriteIconsIndex = 0;
    _mapController.selectedMapData(widget.selectedMap);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {
            Get.back();
          },
          icon: Icon(Icons.arrow_back),
          iconSize: 24,
        ),
        backgroundColor: _appSettings.appBarAndNavColor,
      ),
      body: Container(
        width: _appSettings.appWidth * 0.9,
        height: _appSettings.appHeight - _appSettings.appBarHeight,
        child: ListView(children: [
          Container(
            child: GetBuilder(
              builder: (_) {
                return Text(
                  'map > star1 >' + _mapController.mapData.name.toString(),
                  style: TextStyle(fontSize: 20),
                );
              },
            ),
          ),
        ]),
      ),
    );
  }
}

My flutter doctor output

[√] Flutter (Channel stable, 2.2.3, on Microsoft Windows [Version 10.0.19043.1165], locale ko-KR)
• Flutter version 2.2.3 at C:\Program Files\flutter
• Framework revision f4abaa0735 (8 weeks ago), 2021-07-01 12:46:11 -0700
• Engine revision 241c87ad80
• Dart version 2.13.4

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3) • Android SDK at C:\Users\HOME\AppData\Local\Android\sdk • Platform android-30, build-tools 29.0.3 • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) • All Android licenses accepted.

[√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Android Studio (version 4.1.0) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.59.1) • VS Code at C:\Users\HOME\AppData\Local\Programs\Microsoft VS Code • Flutter extension version 3.25.0

[√] Connected device (3 available) • sdk gphone x86 (mobile) • emulator-5554 • android-x86 • Android 11 (API 30) (emulator) • Chrome (web) • chrome • web-javascript • Google Chrome 92.0.4515.159 • Edge (web) • edge • web-javascript • Microsoft Edge 92.0.902.78

Can anyone provide a solution for this?

97_HYUN
  • 83
  • 2
  • 6

2 Answers2

0

It's most probably you are using SDK of flutter which employs null safety. Edit your pubspec.yaml with fluuter sdk less than 2.12 or ensure null safety in your code.

0

Normally, "Null check operator used on a null value" is throw when

var v = null;
var s = v!.value; <--"Null check operator used on a null value"

You can check the simple flutter APP codes.

Also, this may help: Null check operator used on a null value

Kin Cheng
  • 88
  • 5