Flutter comeup with new stable version flutter 2.0. This stable version supports null safety. What is null safety, means compile-time safety feature we no need to worry about null pointer exceptions while writing the flutter code, flutter will automatically checks null safety. This Null safety will make developer to create more reliable and secure programming code. In this post we are going to cover how to migrate existing flutter app to Null safety
Read about how to upgrade flutter to latest version
Null safety features
To Migrate Null Safety Prerequisites
Let's get started
We will Migrate flutter app to null safety by 3 simple steps
Step 1: First step is to check is the project is migratable or not, because we can't migrate all projects
Step 2: We need to migrate our dependency plugins
Step 3: Call Migrate project
Before start to migrate our app to Null safety first we need to check our installed flutter and dart versions to latest version
Make sure your flutter version to be 2.0 or above and take backup of your current flutter version and project
First check the flutter version by below command
open command prompt or terminal and type command
flutter upgrade |
then hit enter, it will upgrade your version to latest version
Then check your flutter and dart version
Check dart latest version and download
After verifying the flutter and dart version we need to check our dependency plugins are upto date to support null safety
To do this type below command in terminal
flutter pub outdated --mode=null-safety |
Now it will give the all your plugins with current and updates versions
Here all my versions are upto date except my url_launcher, now update this plugin version to latest version 5.7.10 by edit your pubspec.yaml file and change your version to 6.0.2 then get update version by run pub get command
Now we can migrate our project to null safety feature
We have done with 1st and 2nd steps, now it's time to migrate our project with 3rd step
Migrate app to null-safety type below command
dart migrate |
Then if your packages are ready to, then the tool produce a link to check the migration suggestions
View the migration suggestions by visiting:
http://127.0.0.1:54269/D:/Blog%20Notes/Flutter/Workspace/sticky_header/sticky_header?authToken=3wM5xFgrgC8%3D |
Use this interactive web view to review, improve, or apply the results.
When finished with the preview, hit ctrl-c to terminate this process.
If you make edits outside of the web view (in your IDE), use the 'Rerun from
sources' action.
open that url in browser and check there every variable is defined with null safety
For every variable and type annotation, we can see what nullability the tool infers
in the update null safety code it added late for my varibale like below
late Map<String,List<Contact>>hm;
late List<String>alphabets;
|
List<Contact>namesString=hm[alphabets[index]]!
|
Now this exclmation mark tell that these vaibales can not have nullable values anymore
Read more about Migration process at official page
Conclusion: Sound Null Safety reduces mechanical work to make code cleaner and clearer. Null safety increases refactoring of code
Article Contributed By :
|
|
|
|
4170 Views |