iDecode
iDecode

Reputation: 28906

Non-nullable instance field must be initialized

class Foo {
  int count; // Error
  void bar() => count = 0;
}

Why I'm seeing an error when I am already initializing it in the bar method? I could understand this error if count was marked final.

Upvotes: 79

Views: 150771

Answers (5)

NYT_ SKY
NYT_ SKY

Reputation: 29

I'd also like to share my answer, which in my case is a string I was following a much older instructions and didn't know that it had updated.

error was non_nullable, in line 5, the fix was initializing a string value in the string and writer variable. (Hopefully this'll help for those who had same troubles as I did)

class Quote {
  String text = ""; 
  String writer = "";

  Quote(String text, String writer) {
    this.text = text;
    this.writer = writer;
  }
}

Quote myquote =
    Quote("It takes a genius for a man to fear love", "Rick and Morty");

Upvotes: 0

iDecode
iDecode

Reputation: 28906

(Your code was fine before Dart 2.12, null safety)

With null safety, Dart doesn't know if you actually assigned a value to count. Dart initializes objects in two phases, and Dart expects all member variables to already be initialized when the constructor's body executes. Because your members are non-nullable and haven't been initialized to non-null values yet, this is an error.

1. At the time of declaration:

int count = 0;

2. In the initializing formals parameters:

Foo(this.count);

3. In the initializer list:

Foo() : count = 0;

4. Use the late keyword:

This means that you promise that the variables will be initialized before anything attempts to use them.

class Foo {
  late int count; // No error
  void bar() => count = 0;
}

5. Make the variable nullable:

class Foo {
  int? count; // No error
  void bar() => count = 0;
}

However, that will require that all accesses explicitly check that the members aren't null before using them.


Also see: Dart assigning to variable right away or in constructor?

Upvotes: 162

Trophy Developers
Trophy Developers

Reputation: 315

Use the late keyword to initialize a variable when it is first read, rather than when it's created.

class Questionz {
    late String questionText;
    late bool questionAnswer;

    Questionz({required String t, required bool a}) {
        questionText = t;
        questionAnswer = a;
    }
}

Upvotes: 9

iamSri
iamSri

Reputation: 7

IN my case i found giving ? and ! to the variable helpful:

 double? _bmi; // adding ? to the double 

  String calculateBMI(){

    _bmi=weight/pow(height/100, 2);
     return  _bmi!.toStringAsFixed(1);// adding ! to the private variable


}

 String getResult(){
    if(_bmi!>=25){ //adding ! to the private variable
     return 'Overweight';
     } else if (_bmi!>=18.5)
   {
      return 'normal';
    }else{return 'underweight';}

Upvotes: -2

Milind Gour
Milind Gour

Reputation: 131

in pubspec.yaml if you are using : environment: sdk: ">=2.12.0 <3.0.0"

change to environment: sdk: ">=2.7.0 <3.0.0"

2.12.0 null safety feature is on & 2.7.0 null safety feature is off

tip : instead of copy change manually

for more info https://dart.dev/null-safety

for null safety use ? after variable like var a? and while using the variable use ! after variable , like : if(a!){}

Upvotes: -3

Related Questions