Null Safety in Dart, Flutter Essentials

Sound Null Safety in Dart, Flutter essentials.

While working in Flutter you must have come across several NULL errors. To tackle this problem the Flutter and Dart team has announced Sound Null Safety support in Dart.

Null safety means that a variable can not have null or void values. This feature dramatically reduces errors and app crashes. It easily identifies Null-related errors during the development phase, rather than at runtime.

In this post, we will explore the Null Safety Support for Flutter & Dart. We will take a look at how null safety is implemented in Flutter, how it influences the development process, and what are the upsides of using null safety.

Pre-requisitives ๐Ÿ”ฎ

  • Basic knowledge of Dart programming Language
  • Flutter SDK greater than 2.0.0
  • Dart SDK greater than 2.12.0

What is Sound Null Safety? ๐Ÿ”Š

When you opt into null safety, types in your code are non-nullable by default, meaning that variables canโ€™t contain null unless you say they can. Null Safety just ensures that during development & runtime, your non-nullable variable should never contain a null value.

Principles of null safety ๐Ÿพ

Dart null safety support is based on the following three core design principles:

1. Non-nullable by default

Unless you tell Dart that a variable can have a null value, it is non-nullable. They chose this default, as research found that non-null was by far the most preferred.

2. Incrementally Adoptable

You can easily switch to null safety. You can migrate incrementally, mixing null-safe and non-null-safe in the same project.

3. Fully Sound

Dartโ€™s null safety is sound, which enables compiler optimizations. If the system determines that something isnโ€™t null, then that thing can never be null. You experience not only fewer bugs but faster execution too when your dependencies have migrated to null safety.

Non-nullable Types ๐Ÿšซ

By default, all the variables are non-nullable by default. So if you create any variable it can not contain a null value. You can try the tutorial either on Dartpad or using Unit Tests in Flutter. Let’s see an example:

int n;

n = null; 
// The compiler will show error here,
// trying to assign null to a null-nullable variable.

Nullable Types โœ…

In some situations, you may need to store null in a variable, for those cases Dart supports nullable types as well. To declare them just add ? after their data type. A nullable type has the null value by default. Let’s see an example:

int? n;

n = null;
// Compiler Compiles Successfully

Assertion Operator โ—

We use the null assertion operator (!) when we want to treat a nullable expression as non-nullable in Dart. It provides more power to the developer, but make sure to ensure to use it when you are certain a value can not be null.

// Nullable Type
int? n = 30;

// Non-nullable Type
int x = n!;

The compiler doesn’t allow setting a nullable value to non-nullable types without checking if the value is null. So we can force the compiler using the (!) assertion operator. Alternatively, if we use the following code it will also work:

int? n = 30;
int x;

if(n != null){  
// Dart is intelligent enough to know that at this point 'n' can not be null
   x = n;

This capability of Dart is also called Flow Analysis. It is a mechanism that determines the flow of the program and figures out null type errors.

Late Keyword ๐Ÿ•

All the non-nullable variables must be initialized after their declaration. To counter this issue the late keyword is used. It allows lazy initializing a variable. However, it is then the Developer’s job to ensure that it is initialized before it is used.

late int n;

void initVars(){
  n = 10;

Bonus! Useful Operators ๐ŸŽŠ

1. Conditional Member Access (?.)

It is like . , the operator used to access members of a class instance. It just adds the functionality of a null check on the class instance.

// Obtaining value only if object is not-null
2. If null (??)

expr1 ?? expr2

If expr1 is non-null, returns its value; otherwise, evaluates and returns the value of expr2.

// Earlier
int number = value == null ? 0 : value;

// Using If Null operator
int number = value ?? 0;

Conclusion ๐ŸŒผ

In the post, I have explained the basic structure of the Null Safety Support For Flutter & Dart. you can now easily write null-safe code in your Flutter application. This was a small introduction to Null Safety Support, and it’s working for Flutter & Dart.

Next, you should check these posts on Flutter:

If you have any queries you can comment below and I will be happy to help you. If you are new to Flutter check my post on how to install flutter on windows?

Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
FutureBuilder in Flutter

Complete Guide to FutureBuilder in Flutter

Next Post
Streams in Flutter

Streams in Flutter, Simplified

Related Posts