The Error “Null check operator used on a null value”, is an error that is invoked when you are working on flutter and try to run a simple flutter app. Given below is the snippet of the error you might get:
Null check operator used on a null value
I would like to share with you the steps I took to fix the “Null check operator used on a null value”
Why “Null check operator used on a null value” Error is Seen?
The error, “Null check operator used on a null value” is seen because you must have used a bang operator ‘!’ on a nullable instance that was not initialized.
You can look at the below mentioned example code to know exactly which part of your code is causing the error:
String? foo; // Nullable String
void main() {
var len = foo!.length; // Runtime error: Null check operator used on a null value
}
A detailed solution of the error, “Null check operator used on a null value” is given below:
How to Fix “Null check operator used on a null value” Error?
To fix the error, first, you will have to find the bang operator in your code and instead of using a bang operator you can use a local variable, or you can also use ?.
and ??. For FutureBuilder
/StreamBuilder
you have to specify a type or use as to downcast an object to a list or map.
To fix the error, “Null check operator used on a null value”, first, you will have to find the bang operator in your code and then replace it by following one of the methods mentioned below:
Method 1: use a local variable
To fix the error you can use a local variable instead of a bang operator. For your reference you can look at the code mentioned below:
var f = foo;
if (f != null) {
var len = f.length; // Safe
}
This should fix the error “Null check operator used on a null value”.
Method 2: Use ?. and ??
To fix the error you can use ?.
and ?? instead of a bang operator. For your reference you can look at the code mentioned below:
var len = foo?.length ?? 0; // you have to provide a default value if foo was null.
This should fix the error “Null check operator used on a null value”.
If you are using FutureBuilder
/StreamBuilder
If you are using FutureBuilder
/StreamBuilder
you have two ways by which the problem can be solved:
Method 1: Specify your FutureBuilder/StreamBuilder
To fix the error you will have to Specify your FutureBuilder
/StreamBuilder
by following the below mentioned code as a reference:
FutureBuilder<List<int>>( // <-- type 'List<int>' is specified.
future: _listOfInt(),
builder: (_, snapshot) {
if (snapshot.hasData) {
List<int> myList = snapshot.data!; // <-- Your data
}
return Container();
},
)
This should fix the error “Null check operator used on a null value”.
Method 2: Use as
to downcast Object
To fix the error you have to use as
to downcast Object
to the type required by you, it can either be a List
or a Map
. You can do so by following the below mentioned code as a reference:
FutureBuilder<List<int>>( // <-- type 'List<int>' is specified.
future: _listOfInt(),
builder: (_, snapshot) {
if (snapshot.hasData) {
List<int> myList = snapshot.data!; // <-- Your data
}
return Container();
},
)
This should fix the error “Null check operator used on a null value”.
Conclusion
To fix the error “Null check operator used on a null value”, first, you will have to find the bang operator in your code and instead of using a bang operator you can use a local variable, or you can also use ?.
and ??.
For FutureBuilder
/StreamBuilder
you have to specify a type or use as to downcast a object to a list or map.