Skip to content

Migrating to null safety(Flutter)

Flutter, Dart, Null Safety, Flutter Tips3 min read

This month has been a bit of a whirlwind, especially with the release of Flutter 2. One of the biggest changes with Flutter 2 is the introduction of null safety and I've been working with it for the last few days so I decided to write some short notes about some of my observations and about how to actually migrate.

You don't have to migrate yet

First off I think one of the things that isn't highlighted enough is that you don't need to migrate yet. You can use Flutter 2 without migrating to null safety. That being said, it's a good idea to start thinking about it because all the updates to the packages you use from here on out will support null safety(and you probably won't be able to use most of them unless you migrate to null safety).

If you use a lot of packages in your project then you probably won't even be able to properly migrate completely yet. So I think the best way to perform the migration is with the following steps:

  1. Update your project so it will run against Flutter 2.x.x
  2. Upgrade any packages that you can to the latest version(some specific packages seem to work even if you haven't migrated to null safety yet?)
  3. Figure out which packages you use that aren't being maintained anymore and migrate to equivalent packages that are maintained(or migrate them yourself and submit a PR)
  4. Migrate to null safety

It sounds like a lot of work, and to be fair it kind of is, but you could space the above process over a couple of months if it's a project with a lot of packages.

1. Migrating to Flutter 2.x.x

I won't write a lot about this because it's pretty straight forward. Just upgrade to the latest stable version of Flutter. You probably won't have to change very much but you might see a few deprecations that you need to take care of. If you want to be able to switch between Flutter versions painlessly check out my post about FVM here.

2. Upgrade third party packages(that you can)

Like I said some packages might not work with null safety, but others will. Try running flutter pub outdated and upgrade some packages to see if your project builds with the latest version. This will make the steps after this somewhat smoother.

3. Migrate to maintained packages

You might be depending on packages that aren't being actively maintained anymore. If this is the case you should try reaching out to the author about migrating to null safety - if they're busy maybe you could volunteer to do the migration yourself and submit a pull request. If none of this works then you'll either have to maintain a fork of the package yourself or migrate to another package that has the same functionality. The second option probably has the least overhead if an alternative solution already exists. You can actually check which of your packages support null safety using this command: flutter pub outdated --mode=null-safety(you need to be on Flutter 2.x.x for this to work).

4. Migrate to null safety

So this is essentially the last step. To turn null safety on you need to change the dart version restrictions in your pubspec.yaml file under the environment section. It'll probably look something like this:

2 sdk: ">=2.7.0 <3.0.0"

You need to change this to:

2 sdk: ">=2.12.0 <3.0.0"

And then run flutter pub get for good measure.

After you've done this you can use the following command to automatically update all your packages to the latest null safety compatible version: flutter pub upgrade --nullsafety.

The command above won't migrate any of your code. You can use the dart tool to help you migrate your code with the dart migrate command but in my experience it doesn't seem to work that well...It was okay but it makes some weird decisions in some places so you might be better off migrating by hand, especially if you have a bigger project. That's upto you in the end but. You can find a more indepth guide about null safety migration here - all I did was summarize it the best that I could.

Opting out of null-safety

If you change your dart environment constraints to ">=2.12.0 <3.0.0" your project won't run if any of the packages in your pubspec.yaml don't support null safety. There is a way to get around this, but it's not recommended. You just need to add "--no-sound-null-safety" to your flutter run commands, for example flutter run --no-sound-null-safety. Do this at your own peril, but I guess it's good to have the option if there's just one particular package you rely on that isn't maintained and you're in a pickle.