Introduction
Null-aware operators are special Dart operators that handle null values safely. They prevent the dreaded Null check operator used on a null value runtime error and make null-safe code more elegant.
Why It Matters
Dart has sound null safety, meaning a variable cannot be null unless explicitly declared nullable. Null-aware operators let you work with nullable values gracefully — providing defaults, skipping null calls, and assigning values conditionally.
Null-Aware Operators
| Operator | Name | Description |
?? | If-null (null coalescing) | Returns the right side if the left is null |
??= | Null-aware assignment | Assigns only if the variable is null |
?. | Null-aware access | Calls method/property only if not null |
?[] | Null-aware index | Accesses index only if the object is not null |
! | Null assertion | Asserts the value is not null (throws if null) |
Code Example
void main() {
// ?? operator: provide a default
String? username;
String display = username ?? 'Guest';
print(display); // Guest
username = 'Alice';
display = username ?? 'Guest';
print(display); // Alice
// ??= operator: assign if null
int? count;
count ??= 0;
print(count); // 0
count ??= 99;
print(count); // 0 (already assigned, no change)
// ?. operator: safe method call
String? city;
print(city?.toUpperCase()); // null (no error!)
city = 'phnom penh';
print(city?.toUpperCase()); // PHNOM PENH
// Chaining null-aware operators
String? firstName;
int? len = firstName?.length;
print(len ?? 0); // 0
}
Output:
Guest
Alice
0
0
null
PHNOM PENH
0
Explanation
?? returns the left operand if it is not null; otherwise returns the right operand (the default).
??= assigns the right value to the variable only when it is currently null.
?. calls a method or accesses a property on an object only if the object is not null. If null, the entire expression evaluates to null without throwing.
- These operators work together with Dart''s sound null safety type system.
Notes
- The
?? operator is right-associative: a ?? b ?? c is read as a ?? (b ?? c).
- Use
?. when chaining calls on nullable objects: user?.address?.city.
- Avoid overusing the null assertion operator (
!) — if you assert incorrectly, you get a runtime exception.
Common Mistakes
- Forgetting
? in the type declaration (String name vs String? name) — non-nullable variables cannot be null.
- Using
! (null assertion) carelessly — it throws at runtime if the value is actually null.
- Ignoring the return value of
?. — it returns null, so store or handle it accordingly.
💪 Exercise
- Declare a nullable
String? email. Use ?? to print a default message if it is null.
- Declare a nullable
List<int>? scores. Use ?. to safely call .length and print 0 if null.
- Use
??= to initialize a nullable counter to 1 only if it has not been set yet.
🧠 Quiz
1. What does null ?? 'default' return?
- A) null
- B) 'default' ✅
- C) error
- D) false
2. What happens when you use ?. on a null object?
- A) Throws a NullPointerException
- B) Returns null without throwing ✅
- C) Returns false
- D) Skips the entire program
Summary
Dart''s null-aware operators make null handling safe and expressive. Use ?? to provide fallback values, ??= to assign defaults, and ?. to safely chain method calls on nullable objects. These operators are at the heart of Dart''s sound null safety system and help you write robust, crash-resistant code.