Experimenting with Null Safety on Flutter 2.0

Experimenting with Null Safety on Flutter 2.0

A short tutorial on how to migrate your Flutter code or application to Sound Null Safety along with instructions on how to implement it.

Gayathri devi Srinivasan's photo
Gayathri devi Srinivasan
·Mar 23, 2021·

3 min read

Listen to this article

Are you adding conditional statements to handle null exceptions on your code? Here comes the caretaker Sound Null Safety, which will handle all your null exceptions and make code non-nullable by default.

What Is Null Safety?

Null Safety, being the biggest change made to Dart is a compile-time safety feature which eradicates Null in real time increasing efficiency whilst also preventing errors. This sound static type system, according to the official documentation, should come with the following features, making developers lives much easier:

  • It should render the code to safe by default.

  • It should be usable and easy to write.

  • Static checks present in the null safe code are done during run-time to boost efficiency.

So, how can we migrate our app to implement this feature?

Prerequisites

To be able to implement Sound Null Safety in your Flutter apps, you need the basic prerequisites:

  1. Flutter SDK version 2.0.0
  2. Dart SDK version 2.12.0

Implement Sound Null Safety on a BMI calculator app

To implement Sound Null Safety in your Flutter app, you need to follow the following steps:

  • Pub Upgrade

The first step to get into Sound Null Safety is to check if all the packages are updated to match it. If not, we need to bring all the pubs to their updated version.

To check this, run the following command:

flutter pub upgrade --null-safety

On running the above command, you will receive information about the packages that have null safety.

step1.png

The resolvable column above will indicate the version of package that matches the version of null safety being applied.

Once we receive information on the version of packages, considering the need for upgrade, we need to run the following command to complete the `pub upgrade:

flutter pub upgrade --null-safety

To ensure that all your packages are upgraded to null safety, re-run the flutter pub outdated --mode=null-safety command again.

We can now proceed to the next step if the following is displayed on the screen:

step2.png

  • Dart Migrate

To migrate Dart, run the following command:

dart migrate

On running the command, a null preview link will be generated on the console screen that will allow us to inspect changes in our code:

step-3.png

  • Preview and Apply Migration

On clicking the null preview link, we'll be directed to a preview site where we can customise our null safety implementation by selecting and deselecting the checkmarks of the items to change. It also has the option to re-run the application from the preview source. To confirm the changes that have been made, click the button labelled Apply Migration. Once done, your app is ready to be executed with Sound Null Safety.

five.png

Shown above is a Null preview with options re-run from source and apply migration. The left side folder preview allows you to select and deselect changes.

Quick Tips

  1. Wait for your dependencies to be migrated before you start. Run flutter pub outdated --mode=null-safety to find out if you are ready.
  2. Consider using a migration tool to speed up the migration. Run dart migrate and iterate.
  3. Migrating code to null safety is usually pretty straightforward. The sooner you migrate, the sooner you get the benefits.

Conclusion

Sound Null Safety reduces mechanical work for thinking choices in order to make code clearer. It increases refactoring of code, while also guiding the output of the tool. On rare occasions, it does tend to produce errors, added to the fact that process of migration is irreversible. To supplement this, it has been bundled up with great advantages to improve on code quality.

Hope this article has given you clear insights on switching to Sound Null Safety. To read more on the topic, follow the link.

Thank you so much for reading!

 
Share this