How to migrate Dart code to Non-nullable (NNBD)?
I have a Flutter app that was written before null safety was introduced to Dart.
I heard of a Null Safety Migration Tooling and was wondering how I could use it to convert my code from pre-NNBD to Non-nullable by default.
You need at least Dart
2.9. At the time of writing, you can use
2.9.0-10.0.dev, i.e. put the following SDK constraint in your
environment: sdk: ">=2.9.0-10.0.dev <3.0.0"
/*?*/ hint means that something should explicitly be nullable.
/*!*/ means that something should be non-nullable.
You can already add these hints manually before using NNBD. Otherwise, you can add them with the tool during migration (see below).
? makes a type nullable after migration.
_ makes it non-nullable (meaning no character is changed because non-nullable is the default).
! makes an expression non-nullable.
These edits are previewed in the migration tool.
Using the tool
2.9 comes with an
This tool can be used to interactively convert code to NNBD.
I will cover the migration steps described in the README and try to simplify them:
- Go to your project in your command line and run
flutter pub getwhen using Flutter. (Note that at the time of writing, the Flutter SDK is not yet supported)
dart migrate. (Note that at the time of writing, I need to use
- Wait for analysis and migration to complete and then view migration suggestions by opening the URL you see in the command line in your browser (of format
http://localhost:<port>/<project path>?authToken=<token>). You should see something like this:
- Select a file to begin with. It should look something like this:
- View the proposed edits on the right (matching the highlighted characters):
- If you find edits that you think are not correct, find the root cause in Edit Details:
You can scroll down to trace the root expression that led to the edit proposal. When you find some wrong decision somewhere along the way, you either
Add /*?*/ hint or
Add /*!*/ hint as explained above (you can also add them manually in your IDE).
If an edit looks right, you do not need to do anything.
You will probably need to
Rerun From Sourcesa lot. This will apply the new hints you added and any other edits you made to the code and generate new edits. Do this until all edits look right.
Now, you might see your hints highlighted in red, which means that the tool will remove the hints and convert them to either
!, or blank.
You should probably save your project at this point (e.g.
Note: at this point, your code has a bunch of added
/*!*/hints and has not yet been migrated.
Apply Migration: this will apply the proposed edits and remove all hints. Probably leave the migration tool open for now.
Now, you do not have any hints in your code anymore.
Test your project (
flutter pub getand then run).
If something went wrong, it is helpful to have left the migration tool open.
Potentially, you want to revert your version to before the migration and repeat the steps.
Success! Your project is now null safe 🙌🏽