Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

flutter
communitycreator

What is null safety in Flutter?

Amey Sunu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 and null safety

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:

  1. Create a new git branch to push the new migrated project. This is to make sure that none of your project files are affected within the master branch.
git checkout -b null-safety

Here, null-safety is the branch name where the new migrated code will be committed.

  1. Check if all the dependencies within the app can be upgraded. If you don’t have any third-party dependencies, feel free to skip this step. You can check which dependencies can be updated by running:
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.

Build errors on iOS

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.

Migration

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.

Unsounding null safety

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 Google Sign In or Firebase Authenticationwhere, if a user is null, then the app will crash. With null-safety enabled, this error cannot occur during any course of project runtime, neither at build nor at production.

RELATED TAGS

flutter
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring