Non-nullable instance field must be initialized

51,442

Solution 1

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

With null safety, Dart has no way of knowing if you had actually assigned a variable to count. Dart can see initialization in three ways:

  1. At the time of declaration:

    int count = 0;
    
  2. In the initializing formals:

    Foo(this.count);
    
  3. In the initializer list:

    Foo() : count = 0;
    

So, according to Dart, count was never initialized in your code and hence the error. The solution is to either initialize it in 3 ways shown above or just use the late keyword which will tell Dart that you are going to initialize the variable at some other point before using it.

  1. Use the late keyword:

    class Foo {
      late int count; // No error
      void bar() => count = 0;
    }
    
  2. Make variable nullable:

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

Solution 2

These are new rules about Dart about null safety

class Note {
  late int _id;
  late String _title;
  late String? _description;
  late String _date;
  late int _priority;
}

make sure before your variable put late

Share:
51,442
iDecode
Author by

iDecode

When developers want to hide something from you, they put it in docs.

Updated on September 30, 2021

Comments

  • iDecode
    iDecode almost 3 years
    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.

  • iDecode
    iDecode about 3 years
    First, I have already mentioned about the late keyword, second there is no reason to put multiple variables to show the same thing.
  • Musfiq Shanta
    Musfiq Shanta almost 3 years
    i saw it when i was coding.that's why i put it and mention
  • iDecode
    iDecode almost 3 years
    What did you see, late keyword? It's already there in my answer. Your answer isn't providing anything new. So, you can think of deleting it (in a good spirit) but the choice is always yours.
  • iDecode
    iDecode almost 3 years
    I have already mentioned this in my answer. Sorry, there's nothing new in your post.
  • iDecode
    iDecode almost 3 years
    So, you're basically telling me to opt out of null safety? Sorry, this isn't something I was looking for !
  • Milind Gour
    Milind Gour almost 3 years
    your call bro ...I didn't want null safety in project amd got the same error
  • Milind Gour
    Milind Gour almost 3 years
    for nulll safety use ? after variable like var a? and while using , use ! after variable ,like : if(a!)
  • iDecode
    iDecode almost 3 years
    First, how long can you stay there without null safety? Second, I've already provided that solution in my answer.
  • Milind Gour
    Milind Gour almost 3 years
    my error was , due to await and then in ascyn function , I was getting value from another class , which returns future , soo the error was you can't return a variable with ? because of await and then ....
  • Admin
    Admin almost 3 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • Jeremy Caney
    Jeremy Caney over 2 years
    It appears this answer was flagged as Not an Answer. It very much is an answer. It is an answer with undesirable side effects—but that's why it's been downvoted, which is the correct action for answers that aren't useful. Answers that provide a sincere effort at answering the question should not be deleted (unless, of course, they are just a link).