What are null-aware operators in Dart?

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 null-safety principlesA variable can't have a null or void value.. Dart allows us to denote nullable variables using the “?” symbol during initialization, such as 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.

1. Null-coalescing operator (??)

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: null
print(null ?? 100); // Output: 100
String? username; // Nullable username
// Using null-coalescing operator to provide a default value
String defaultUsername = username ?? 'Guest';
print('Welcome, $defaultUsername'); // If username is null, it uses 'Guest' as a default value
}

2. Null-coalescing assignment operator (??=)

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 variable
print(value); // Output: null
value ??= 100; // If value is null, the operator will store 100 in the variable.
print(value); // Output: 100, changed from null
}

3. Conditional property access operator (?.)

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 nullable
print(value); // Prints "null"
// Use the null-aware operator to call toLowerCase() safely
print(value?.toLowerCase()); // Prints "null"
// print(value.toLowerCase()); // Throws error.
}

4. Null-aware spread operator (...?)

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.

print(null ?? null); // Output: null


print(null ?? 100); // Output: 100


Null-coalescing assignment operator (??=)


It assigns a value to a variable if the variable is currently null. If not null, it remains unchanged.


value ??= 100; // If null, assigns 100

Conditional property access operator (?.)

It safely accesses an object’s properties, methods, or elements.

print(value?.toLowerCase()); // Safely calls toLowerCase()


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.

final spreadNumbers = [...?numbers]; // Safely spreads elements

Take this quiz to test your knowledge of Dart’s null-aware operators.

Dart null-aware operators

1

What is the result of the following Dart code?

int? x;
int y = x ?? 5;
print(y);
A)

The code will result in a compilation error.

B)

The code will print “null.”

C)

The code will print “5.”

D)

The code will print “0.”

Question 1 of 40 attempted

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved