Dealing with nulls in Dart
Tags: flutter, dart, null
Published: 2020-04-15 15:30:21

Despite its young age and influence from other programming languages, Dart doesn’t really provide a null safety. Even classic Optional from Java doesn’t exist in Dart, even though is so simple to implement. A lot of people coming from Kotlin or Swift may see this as a huge disadvantage.

But that may change since couple days ago **Dart 2.7** was released and with it — comes the preview of null safety. It drastically changes how types interact with nulls. It’s still an **early preview** so don’t expect too much, but the idea will probably remain the same. You can track the progress of this [feature](https://github.com/dart-lang/language/issues/110) and read the [docs](https://github.com/dart-lang/language/blob/master/accepted/future-releases/nnbd/roadmap.md) behind it.

So let’s explore all the tricks Dart has to make your life easier when dealing with nulls!

Before we start: you can find the code executable here on [DartPad](https://dartpad.dev/1b123ec2a2923a27ab667ed4f4255e9e)

## Null coalescing operator
Null coalescing operator or how Dart docs are calling it — if null operator is a double question mark `??` that checks if the object is non-null if it is — it returns its value, otherwise, it evaluates and returns the value of the second expression.

It is commonly used when applying default values or when [merging objects](https://github.com/flutter/flutter/blob/d345a3b303ce041846ff895eb49a104bef133c4b/packages/flutter/lib/src/material/text_theme.dart#L212). I will take hello world as an example:

{% gist https://gist.github.com/pr0gramista/04c3789a5abeab744274e855b031e83e %}

`??` operator helped us prevent `ArgumentError` by supplying a default string. We can even make it better with the assignment version of that operator.

{% gist https://gist.github.com/pr0gramista/c6c247c6194ba60e8271f3d377626972 %}

But of course, this example is a bit stupid since we can just put the default value of the parameter…

{% gist https://gist.github.com/pr0gramista/68a8557df7990e59bca96f2f710c9589 %}

## Conditional member access
`??` is useful, but the most common error we get with null is `NoSuchMethodError`. We get that because we are trying to access a member on null. To avoid that we can use `?.` for accessing members, which will check if the object is null for us. If the object is null then the expression will also return null.

{% gist https://gist.github.com/pr0gramista/aff3fee623c94885b2ba402e329f122b %}

In case our email won’t match we would get an error, but thanks to `?.` on it we will just get null, the same as if there was no `group(1)`.

Of course, we can still stack `?.` like normal member access, but we should be aware that if we want to avoid errors we need to use them on **every** call.

{% gist https://gist.github.com/pr0gramista/91e8c0dfe031127a6ecca3ecc9970852 %}

## Null-aware spread operator
The last trick is so unique that it will probably become the classic job interview question.

{% gist https://gist.github.com/pr0gramista/c131e1c0b91c5fbe2819cd5db3ef7515 %}

It just doesn’t spread if the object is null.

![Meme](https://dev-to-uploads.s3.amazonaws.com/i/o8f431n89rgw1b4vbpdj.jpeg)

## Null safety
All the features I mentioned above don’t protect you against nulls, but rather make it convenient to handle. This changes with null safety, which will change how types work. That can be previewed in Dart 2.7, but don’t even try to use it normally yet.

So TLDR: `int` will become **non-nullable type** and new `int?` will be nullable, which will require a check before accessing the data. You will have to opt-in for that behavior and libraries/dependencies can opt-in separately.
You can check the examples on this [special DartPad](https://nullsafety.dartpad.dev/1ede47fde8adf5d4dab09bcf6ddf1ec6). You can also run Dart 2.7 with `--enable-experiment=non-nullable` to actually compile it.

## Summary
Dart at this moment might not have the most secure typing system, but it is good enough. The tricks I just showed you will probably spare you many `if (x != null)` lines and that’s the point of this post — to avoid writing Dart like Java from 2011.

Dart is also constantly evolving and with null safety on the horizon, it seems to be a great future for Kotlin and Swift developers to move to Dart.
Happy coding!