Non-nullable instance field must be initialized
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:
At the time of declaration:
int count = 0;
In the initializing formals:
Foo(this.count);
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.
Use the
late
keyword:class Foo { late int count; // No error void bar() => count = 0; }
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
![iDecode](https://i.stack.imgur.com/8Jlh3.png?s=256&g=1)
iDecode
When developers want to hide something from you, they put it in docs.
Updated on September 30, 2021Comments
-
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 ifcount
was markedfinal
. -
iDecode about 3 yearsFirst, I have already mentioned about the
late
keyword, second there is no reason to put multiple variables to show the same thing. -
Musfiq Shanta almost 3 yearsi saw it when i was coding.that's why i put it and mention
-
iDecode almost 3 yearsWhat 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 almost 3 yearsI have already mentioned this in my answer. Sorry, there's nothing new in your post.
-
iDecode almost 3 yearsSo, you're basically telling me to opt out of null safety? Sorry, this isn't something I was looking for !
-
Milind Gour almost 3 yearsyour call bro ...I didn't want null safety in project amd got the same error
-
Milind Gour almost 3 yearsfor nulll safety use ? after variable like var a? and while using , use ! after variable ,like : if(a!)
-
iDecode almost 3 yearsFirst, how long can you stay there without null safety? Second, I've already provided that solution in my answer.
-
Milind Gour almost 3 yearsmy 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 almost 3 yearsYour 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 over 2 yearsIt 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).