Migrate to flutter null safety safely
How to safely migrate to null safetyLast updated: July 22, 2021
Null safety is the solution to the billion doller mistake and it’s available for Flutter as well, why live with billion doller mistake anymore!
As null safety is available with Flutter 2.0 It comes with pretty handy migration guide and can simply use the tool to migrate to null safety nicely.
But in reality it’s not that straight forward as it sounds for a medium to big projects, also there might be lots of third party dependencies and tool may produce lots of compile error (atleast it happened with me).
I have waited few months before migrating to null safety for reasons like most of my depedency were not compatible with null safety yet when Flutter 2 just got released and I wanted to see how other people are doing, what kind of problem they face and how that can be solved.
Now is good time to migrate to null safety as most of the popular dependecies already has support for null safety.
Let’s start
To make the process smother we will go through the migration process step by step.
Create a new branch
git checkout -b null-safety
and make sure to commit after each step.
Dependency upgrade
Before migrating to null safety, it’s better to upgrade all the dependencies first.
dart pub outdated --mode=null-safety
I would rather wait for sometime if majority of the dependencies doesn’t have null safety support.
and if dependencies have null safety support then run
dart pub upgrade --null-safety
then simply run
flutter pub get
now check if project has any compile error or not and if app properly run or not.
flutter analyze
If there are any error fix it and run the project. And for iOS you may need to refresh pods, to do that
cd ios
rm Podfile.lock
pod repo update
also delete derived data and make sure for iOS’s minimum deployment target of iOS 10.0 on XCode project and on Podfile
platform :ios, '10.0'
Make sure to commit before proceding to the next step.
Migration
Now run
dart migrate
follow the steps there and it should provide an URL and on there check if the tool correctly migrated or not, if everything is good then apply changes.
Now you should have if not many atleast few compile errors, go through them and fix them individually one by one, such as
- A variable was wrongly made nullable.
- A function parametre takes non null value but nullable value was provided
- A variable was declared as late but later it wasn’t initialized properly.
- A vairable is nullable but the tool made it required.
It’s better to go through by the entire project’s codebase.
After fixing those now run
flutter analyze
After fixing all those errors, run the app to see everything works or not. If not find and fix those issues as well.
Make sure to commit before proceding to the next step.
Code Improvement
As entire codebases is going through changes, it’s better make the entire codebase healthy as much as possible, there might be deprecated codes, improper formating, linting issue. For that Flutter team provides excellent tool called Flutter Fix.
To run the tool project wise from project root folder to changes
dart fix --dry-run
and to apply changes
dart fix --apply
Now the project is ready to test.
Test
Once all the code changes are done and there no more issues left it’s better to check if all the features correctly works or not.