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.
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:
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.
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.
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.
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).
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:
1environment:2 sdk: ">=2.7.0 <3.0.0"
You need to change this to:
1environment: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.
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.