Hello flutter geeks, today we will be learning about Flutter's null safety concept. Given the recent updates to flutter and dart, you would agree that null safety is a must know. This article serves as a basis for just that, so let's dive in. The article contains the following:
- What is null safety?
- Importance of null safety
- Design concepts of null safety
- Migrating to null safety
- Conclusion
What is null safety?
Null safety refers to variables which are accepted to be “null”. That is, they can exist with no direct value assigned to them. So instead of them holding garbage values, they can hold the value “NULL”.
When you opt into null safety, all data types in your code are non-nullable by default, meaning that variables cannot contain “NULL” unless you say so. If we want to specify that a variable can be null, we use a “?” symbol. In this case, if we ever reassign it to “NULL”, it will be valid. Here is a quick example:
String? Hello = “hello”;
Hello = null;
Null safety is fully supported as from flutter 2, and Dart 2.12 or later versions.
Importance of Null Safety.
• Null safety speeds up development time.
• Code maintenance becomes much easier, because developers can spend fewer hours looking for errors caused by “NULL”, which used to come up during runtime.
• Enables special static checks and compiler optimization to guarantee that null-dereference errors won’t appear at runtime.
“ A null pointer dereference occurs when a program attempts to read or write to memory with a NULL pointer. Running a program that contains a null pointer dereference generates an immediate Segmentation fault error. See here for more details. “
Null Safety Concepts.
- Non-nullable by default: Except you tell Dart that a variable can be “NULL”, it will be considered non-nullable. Given the following declarations:
var Widget = Text(“HelloWorld!”);
Dart will ensure that you never assign a “NULL” value to the Widget variable. - Fully Sound: Dart’s null safety is said to be Sound. This implies that we can trust that if it discovers something isn’t “NULL”, it can never be null.
- Incrementally Adoptable: This implies you get to choose WHAT to migrate to null safety and WHEN. You can migrate incrementally, mixing null safe and non- null safe code in the same project.
Migrating Your Project to Null Safety.
Migrating an app is technically the same as migrating a package. To migrate a package or essential application to null safety, follow these five phases:
• Check if your dependencies are ready: It is advised to migrate your depencies incrementally, with the leaves of the dependency diagram being moved first. For example, if package C depends on B, and B depends on A, you are to migrate A to null safety first, then B, then C.
You can — and should — migrate your package before packages that depend on it are migrated.
How to check and update your package dependencies?
- Switch to the latest stable Dart release. Check that you have Dart 2.12 or later;
$ dart --version
- Check dependency status. Get the migration state of your package’s dependencies, using the following command:
$ dart pub outdated --mode=null-safety
If the output says that all the packages support null safety, then you can start migrating. Otherwise, use the Resolvable column to find null-safe releases, if they exist
- Update dependencies. Before migrating your package’s code, update its dependencies to null-safe versions:
Rundart pub upgrade --null-safety
to upgrade to the latest versions supporting null safety.
Note: This command changes your pubspec.yaml file.
Rundart pub get.
• Migrate: You have two options for migrating:
- Use the migration tool, which can make most of the easily predictable changes for you.
- Migrate your code by hand.
For additional help while migrating code, check the following link.
• Analyze: Update your packages (using pub get in your IDE or on the command line). Then use your IDE or the command line to perform static analysis on your code:
$ dart pub get
$ dart analyze
# or flutter analyze
• Test: If your code passes analysis, run tests:
$ dart test
# or flutter test
You might need to update tests that expect null values.
• Publish: It is advised to publish packages as prereleases as soon as you migrate. Firstly, set the SDK constraints to the tested beta version. And then set package version to indicate a breaking change. See here for more details.
Conclusion
I hope you found this article useful and helpful. If you have any questions, please let me know in the comments.