Flutter: Null Check Operator used on a Null Value [Solved]

Today you will learn how to solve the null check operator used on a null value error.

Solution 1 – Upgrade flutter to the stable version

First, make sure that you have the latest stable version of flutter.

Open your terminal/cmd

and run channel stable

Then run flutter upgrade

Make sure you have the latest version of the packages

Run dart pub outdated

This will show you about the outdated packages

Run dart pub upgrade

This will update your packages to the latest version.

Before building your app

Run flutter clean

And again get all the packages.

Save and build the app.

Hopefully, your problem will be solved.

Solution 2 – Make your project Null Safe

Use this official guide about null safety migration from the flutter official website, and make your project null safe. It will help you to identify errors and hopefully your problem will be resolved.

Solution 3 – Be Careful when using the bang operator (!)

You are facing this error because you’re using a bang (!) operator inside your flutter app.

String? name; //Here name is a nullable variable

void main(){
var len = name!.length;
}

Now you get this null check error because you are using bang operator (!) to suppress the error. But on run time when your app runs you face this error.

Solution:

The best solution is to check that the

variable has a non-nullable value

String? name; //Here name is a nullable variable

void main(){
if(name !=null){
var len = name!.length;
  }
}

Or use ?. and ??

String? name; //Here name is a nullable variable

void main(){
var len = name?.length?? 0;
}

Solution 4 Beside Dynamic use a Specific Type

When working with the async data try to use a specific data type and try to reduce the use of dynamic Or Object data type when using data between screens.

Conclusion

Hope this article will help you to debug null value errors. Thanks for reading.

Hussain Humdani

Hussain Humdani

while ( ! ( succeed = try() ) );