Null-aware operators in Dart are essential because they address the need to handle null variables effectively, reducing the risk of unintentional runtime errors and simplifying code complexity while adhering to int?
, String?
, and List?
. This notation makes it explicit that these variables can be null.
Dart employs nullable types to prevent errors when variables are declared as null. These nullable types are denoted by adding a “?” to the type declaration, providing a clearer way to handle null values. This feature helps developers write more robust code by explicitly stating which variables can contain null values.
Now, let’s delve into null-aware operators to understand how they enhance null safety and improve code readability.
??
)The null-coalescing operator ??
is a handy tool in Dart. It helps by letting us set a default value when the left side of the expression is null. If the left side is null, it gives us the value on the right side of the expression, and if it isn’t null, it gives us this left-side value.
For instance, in the expression null ?? 5
, since the left side is null
, the null-coalescing operator gives us the default value on the right, which is 5
.
Here is the code example for the null-coalescing operator. Try changing variable values to get a better understanding of the operator.
void main() {print(null ?? null); // Output: nullprint(null ?? 100); // Output: 100String? username; // Nullable username// Using null-coalescing operator to provide a default valueString defaultUsername = username ?? 'Guest';print('Welcome, $defaultUsername'); // If username is null, it uses 'Guest' as a default value}
??=
)The null-coalescing assignment operator is denoted by ??=
and assigns a value if the variable is null. If the variable isn’t null, it remains unchanged.
Here is the code example for the null-coalescing assignment operator. Try changing variable values to get a better understanding of the operator.
void main() {int? value; // Nullable variableprint(value); // Output: nullvalue ??= 100; // If value is null, the operator will store 100 in the variable.print(value); // Output: 100, changed from null}
?.
) The conditional property access operator is denoted by ?.
and is used to safely access an object’s properties, methods, elements, or a list only if the object itself isn’t null. If the object is null, it returns null instead of throwing an exception.
Here is the code example for the conditional property access operator. Try changing variable values to get a better understanding of the operator.
void main() {String? value; // Declare value as nullableprint(value); // Prints "null"// Use the null-aware operator to call toLowerCase() safelyprint(value?.toLowerCase()); // Prints "null"// print(value.toLowerCase()); // Throws error.}
...?
)The null-aware spread operator is denoted by ...?
and is combined with the spread operator (...
) to spread elements of an iterable only if the iterable isn’t null. If the iterable is null, it results in an empty list.
Here is the code example for the null-aware spread operator. Try changing variable values to get a better understanding of the operator.
If we use List<int>? numbers = [1, 2, 3];
instead of an empty list like List<int>? numbers;
, this will generate the resultant output, but a warning will also be generated as Dart will expect a null value when a null-aware spread operator is used.
void main() {List<int>? numbers;// List<int>? numbers = [1, 2, 3];List<int> combined = [0, ...?numbers];print(combined);}
Note: Visit this answer to better understand the difference between spread and null-aware spread operators.
We can best summarize the use of Dart’s null-aware operators in the table below.
Operator | Description | Example Code |
Null-coalescing operator ( | It provides a default value when the left-hand side of the expression is null. |
|
Null-coalescing assignment operator ( | It assigns a value to a variable if the variable is currently null. If not null, it remains unchanged. |
|
Conditional property access operator ( | It safely accesses an object’s properties, methods, or elements. |
|
Null-aware spread operator ( | It spreads the elements of an iterable (e.g., list) only if the iterable isn’t null. It results in an empty list if null. |
|
Take this quiz to test your knowledge of Dart’s null-aware operators.
Dart null-aware operators
What is the result of the following Dart code?
int? x;
int y = x ?? 5;
print(y);
The code will result in a compilation error.
The code will print “null.”
The code will print “5.”
The code will print “0.”
Free Resources