Upgrading your Flutter app to use Null Safety

Stay Safe

Null Safety is a feature in some programming languages which guarantees that no variable in your code can contain a null value, unless you explicitly allow it. In other words, it makes your program less likely to encounter a Null Reference error during run-time. Null Safety has been added to the Dart programming language in version 2.12.0. This means that you can upgrade your Flutter apps to use Null Safety by following a few steps. I will outline the steps needed for upgrading your Flutter app to use Null Safety in this blog post.

Before upgrading

Before you upgrade your code to use Null Safety, there are some implications that you need to consider. The first implication is that the way you declare your variables will be different. For example, the below statement would not compile in a null-safe program:

int i;

Instead, you have to initialize the variable at the same time you declare it. So, the above line would have to look something like this:

int i = 0;

This will ensure that the variable i will never be null.

However, there may be cases when it’s not possible to set an initial value for a variable during declaration. Or, you may need your variable to contain a null value in certain cases. In such scenarios, you can declare a variable as nullable. Meaning, the variable is able to hold a null value. This can be done by declaring the variable like this:

int? i;

The ? after the type of the variable indicates that the variable is nullable.

Caveat

The caveat here is that nullable variables can potentially cause a Null Reference error during run-time. So, you have to be very careful when dealing with these variables. In fact, the compiler does not let you access these variables unless you explicitly tell it that you’re sure that the value is not null. For example, after declaring a variable like this int? i; the below statement would not compile:

int x = i + 5;

The above statement would not compile because the value of i could be null during run-time. If you are sure that at this point in the code, the variable i cannot be null, you can force the above statement to compile like this:

int x = i! + 5;

Adding the ! symbol tells the compiler to skip the null safety check and allow the code to run. Again, you have to be very careful with the above statement, because if the value of i is null at run-time, the program would encounter a Null Reference error.

The second, and perhaps more important, implication to consider before upgrading your Flutter app to use Null Safety, is that all of the packages that you use in your app must also be Null Safe. If any of the packages does not use Null Safety, then you cannot upgrade your code.

Checking your packages

To check if all the packages that you use in your app use support Null Safety, you can run this command:

dart pub outdated --mode=null-safety

The above command will list all of your dependency packages and show you if the current version of the package supports Null Safety or not. Also, if there’s a newer version of the package that you’re not using, this command will let you know whether that version supports Null Safety or not.

Once you confirm that all your packages support Null Safety, or upgrade to versions that do, you can start the process of upgrading your app. If even the latest version of any of your dependency packages does not support Null Safety, you can always write to the developers of the package and ask them to upgrade.

Upgrading your app

The first step is to make sure that you’re using Dart version 2.12.0 or later. To know which version of Dart you’re using, run this command:

dart --version

If you see that you’re running an older version, you can upgrade to version 2.12.0 by going to your pubspec.yaml and changing the environment entry to look something like this:

environment:
  sdk: ">=2.12.0 <3.0.0"

After you change the version and complete your first build, it is very likely that you will receive a lot of compilation errors. Don’t worry, this is very normal when first making the upgrade. The next step would be to fix all of these errors. Make sure that all of your variables are initialized during declaration (recommended) or declared with the ? symbol if initialization is not possible.

Conclusion

Null Safety allows you to write code that is less likely to run into errors during run-time. Dart version 2.12.0 and later is now one of the programming languages that support Null Safety. To take advantage of this feature in your Flutter apps, make sure you upgrade to version 2.12.0 and then modify your code to comply with the rules of Null Safety. You can find more information about the upgrade process in this article. If you have any questions, or you run into any issues while upgrading, please feel free to leave a comment here or send me your question through the contact page.

Author: Ahmed Haboubi

Ahmed is a Founder and Senior Software Developer at W3 Applications Inc. He brings with him more than 25 years of experience in the field of web and mobile applications development.

Leave a Reply