Null Aware Operators & Null Safety in Dart

Hussain Habibullah
Flutter Community
Published in
4 min readDec 20, 2022

Like other languages, Flutter Dart also provides null-aware operators to make us deal with nullable variables. Let’s start understanding them with this piece of code:

Safe Navigation Operator

This code is pretty straightforward, I made two nullable Strings — which means two strings that are allowed to hold null values. When using a .toUpperCase() method on these Strings, we see this compile time error. This error makes sense because that’s what null safety is all about. Dart is telling you that this variable could be null at this point, so you should handle it before it compiles. And, to handle it for null values, Dart provides you with null-aware operators.

1) Safe Navigation Operator ( ?. )

As a solution to the problem above, we will use a Safe Navigation Operator. We will navigate to the .toUpperCase() method safely by changing an . operator with .? — Easy, right?

Safe Navigation Operator

Here’s another code example, where you might wanna use a safe navigation operator:

Safe Navigation Operator — Example 2

Similarly here, appUser, address, country, and city - are nullable hence Dart is showing a compile-time error for us to handle them.

Just as you expected, replacing every . with safe navigation operator ?. fixes the problem.

One may ask, Hussain, what if I change it with the bang operator ! instead? Like this:

Well, well, well, it will crash on run-time, the reason brings us back to null safety principles. By adding ! operator, you told Dart — Hey Dart, I assure you this variable will never be null, if it is, feel free to throw an error. Dart did exactly what you said. Bang!!

You see the red screen on Flutter with the famous error:

Null check operator used on a null value

2) Default Null-Aware Operator ( ?? )

This operator is also called the (if-null) operator, and easy as it sounds, it checks if a variable is null or not.

Default Null-Aware Operator

The code snippet above is exactly the same as this:

Without Default Null-Aware Operator

You can also use the default operator ?? with a combination of safe navigation operator ?. — Like, in the code snippet below, it will print ‘Hello’ if any of the nullable variables has null.

3) Null-Aware Spread Operator ( …? )

Null-Aware Spread Operator

How would you solve this compile time error? Well, by changing the spread operator ... with the null aware spread operator ...?

With Null-Aware Spread Operator

This operator will add secondList to thirdList only if it's not null. The code above is exactly the same as:

Without Null-Aware Spread Operator

4) Fallback Assignment Operator ( ??= )

This assigns a value if the variable is null. In the code below, it assigns 3 to the variable myNumber if it’s null.

It’s all interesting if you understand it right!

That’s all we have for null-aware operators, see you in the next one!

I am open to freelance, full-time, or part-time roles, feel free to reach out at Linkedin, thank you!

--

--