Nullability of types is an important part of Dart’s type system. In order to simplify the work with nullable types in the Dart language there are
many null-aware operators (??
, ??=
, ?.
, !
, ?..
, ?[]
, ...?
).
Using those operators will make your code more clear and concise.
The If-null operator (??
), will check if the value on the left side is null
, and, if yes, will return the expression on
the right. This is an easy way to set default values instead of dealing with null
. Thus, this operator doesn’t make sense if used with
null
on either sides.
For example, in null ?? x
the x
will always be returned. Using the operator here only introduces confusion and additional
cognitive load. In case of x ?? null
, again the result will always be the value of x
. In both cases the operator can be
replaced with just x
.