When a developer adapts to Null Safety, all the types in the program are non-nullable by default. With Null Safety enabled, you will no longer have any runtime null errors.
Null Safety can be high of value when your app is in production because it makes sure your app won’t crash due to nullable errors.
To indicate that there might be a null value within the variable, ?
is used.
int? a = null;
Flutter 2.0 now supports Null Safety; luckily, migrating previous projects to Null Safety is as simple as running dart migrate
in your project root.
But, before you do this, there are certain steps you need to take care of:
git checkout -b null-safety
Here,
null-safety
is the branch name where the new migrated code will be committed.
dart pub outdated --mode=null-safety
You will now be able to see the packages that can be safely migrated within the produced output. If any package that cannot be migrated persists, then you may have to find an alternative at pub.dev.
You can upgrade all the package dependencies by running:
dart pub upgrade --null-safety
Your pubspec.yaml
file will be successfully upgraded with the new versions of the current packages when null-safety is enabled.
If you have previously installed any iOS packages using Cocoapods, then you need to update your Podfile.
I usually prefer deleting Podfile as a whole and then reinstalling the Pods, as this is more effective and will help resolve any package version conflicts.
Navigate to your ios
folder within the Flutter project, and run:
rm Podfile.lock
pod install
pod repo update
If needed, change your Deployment Target.
Now that you are done with build errors and fixes, run:
dart migrate
Your entire project will be migrated to null-safety
and all the packages and Dart SDK version will be updated as needed.
You might come across certain scenarios where you will need to remove null-safety within a migrated project. You can do this by running:
flutter run --no-sound-null-safety
This will ensure that you can run all mixed packages(null-safety and non-nullable) within a project.
An example of a null safety is any project that uses null-safety
enabled, this error cannot occur during any course of project runtime, neither at build nor at production.
RELATED TAGS
CONTRIBUTOR