With the release of Dart 2.12 & Flutter 2, sound null safety is finally here! What are the benefits, and how do you migrate to null safety? It was all new for Jesper, who decided to read up on the subject and write a few blog posts (start with part 1, part 2) about what he learned.
Migrating your dependencies and code to nul safety is quite easy, and Google has done a good job documenting it here: https://dart.dev/null-safety/migration-guide. I’ll try to do a more straight-to-the-point explanation of the processes, but recommend that you also read the documentation if this is your first time.
The basic steps for migration, as described in Google docs:
- Wait for the packages that you depend on to migrate to null safety.
- Migrate your application’s code, preferably using the interactive migration tool.
- Statically analyze your application’s code.
- Test to make sure your changes work.
One key thing to remember is that migrating an application is technically the same as migrating a package. So before you start to migrate your application, make sure that all of your dependencies are ready for null safe.
You can migrate to null safety before your dependencies support null safety, but you may have to change your code when your dependencies finally support null safety. Dart and Flutter core libraries are null safe and are still usable by applications that haven’t migrated to null safety.
Step 1: switch to Flutter 2.01 and Dart 2.12
Run command ”flutter –version” to output your current versions;
As you can see from the image, I have Flutter SDK version 1.22.6 with Dart SDK version 2.10.5.
To upgrade into Flutter 2 with Dart 2.12, you simply run “flutter upgrade”.
The result after the upgrade with “flutter –version”:
As you can see, my Flutter SDK version is 2.0.3 and Dart SDK version 2.12.2, which means that I’m ready to continue!
Then you need to set your minimum Dart SDK version to 2.12 in your pubspec.yaml file:
Step2: Check the status of your dependencies
To get the null safety migration state of your application’s dependencies, run the following command: “dart pub outdated –mode=null-safety”
From the image above, we can see that both “get” and “percent_indicator” dependencies are not null safe, but have prereleases (green text) that are null safe.
If the output says that all the packages support null safety, then you’re ready to migrate!
Otherwise, upgrade dependencies if possible using the command output.
One important thing to remember about dependencies & null safety:
“When all of an app’s direct dependencies support null safety, you can run the app with sound null safety. When all the dev dependencies support null safety, you can run tests with sound null safety.
You might also need null-safe dev dependencies for other reasons, such as code generation.”
Step 3: Update dependencies
Before migrating your application’s code, update all the dependencies to the latest versions which supports null safety: “dart pub upgrade –null-safety”
As you can see from the image, “get” and “percent_indicator” dependencies have been upgraded. We can even see from the version names that they support null safety.
Then we need to run “dart pub get” to actually fetch the dependencies.
Now when I run “dart pub outdated –mode=null-safety” I get the following result:
Migrating your code
Most changes will likely revolve around inserting the ? or late keywords, or perhaps missing required keywords for non-nullable named parameters.
You have two options when it comes to migration:
- Use the migration tool, which can resolve many of the basic nullable cases.
- I’ll refer you to Googles documentation for this, as it’s quite detailed: https://dart.dev/null-safety/migration-guide#migration-tool
- Migrate your code by hand.
- This is quite straightforward
Migrating by hand
Google recommends that you first migrate leaf libraries, you can find more information about that here: https://dart.dev/null-safety/migration-guide#migrating-by-hand
To migrate a package by hand, follow these steps:
- Edit the package’s pubspec.yaml file, setting the minimum SDK constraint to
- sdk: ’>=2.12.0 <3.0.0’
- Run “dart pub get” (running “dart pub get” with a lower SDK constraint of 2.12.0 sets the default language version of every library in the package to 2.12, opting them all into null safety)
- You’ll probably see a lot of different analysis errors in your application, and that is completely normal.
- Migrate the code of each Dart file to null safe using the analyzer to identify all the problems (adding ?, !, .?, required or late keywords, null checks etc.)
Analyze your code in order to check for possible errors by running “flutter analyze” or “dart analyze”
Test your code
Remember to also fix your test files so that they are null safe, and then running all the tests to ensure that everything works as it should by running e.g “dart test”.
That’s it, hopefully, you’ve found this article helpful! Migrating to null safety is quite easy (depending of course on the complexity of your project) and provides many benefits.
Thank you for reading!