Dart's Null-Aware Operators: Safely Handle Nulls

Null-aware operators let you work with potentially null values without a cascade of if (x != null) checks. Use them to access properties, provide defaults, or assign values only when a variable is null. The footgun is confusing safe ?. with unsafe !.
Why it exists
To solve the "billion-dollar mistake" of null references. Before sound null safety, a null value could sneak into any variable, leading to unexpected runtime crashes. Null-aware operators provide an ergonomic, readable way to handle values that might legitimately be null.
The mental model
Think of a null-aware operator as a conditional question asked before an operation. The ?. operator asks, "Is this object null? If so, stop and return null. If not, proceed with accessing the property." This prevents your code from attempting to call a method on a non-existent object, short-circuiting the entire expression safely.
How it works
Dart provides several key operators. First, the null-aware access operator (?.) short-circuits an expression: myObject?.someProperty evaluates to null if myObject is null, otherwise it evaluates to myObject.someProperty. Second, the if-null operator (??) provides a default value: value ?? 'default' returns value if it's not null, otherwise it returns 'default'. Third, the null-aware assignment (??=) assigns a value only if the variable is null: myVar ??= 'newValue' is shorthand for if (myVar == null) { myVar = 'newValue'; }.
When to use it
Use these operators whenever you expect a variable might be null as a normal part of your program's flow. They are perfect for chaining method calls on nullable objects, providing default configuration values, or lazily initializing variables. This is common when parsing JSON that might have missing keys or working with optional data from an API.
When not to use it
Avoid using the related not-null assertion operator (!), also called the "bang operator". It's a footgun that tells the compiler you guarantee a value isn't null. If you're wrong, your app will throw an exception at runtime. Overusing ! defeats the purpose of null safety. If a value should never be null in the first place, declare its type as non-nullable (e.g., String instead of String?).
One canonical example
Imagine fetching a user profile that might not have a bio. Without null-aware operators, you'd write: String userBio; if (user != null && user.profile != null && user.profile.bio != null) { userBio = user.profile.bio; } else { userBio = 'No bio provided'; }. With them, the code becomes a single, readable line: final userBio = user?.profile?.bio ?? 'No bio provided';. This is safer and far more concise.
Interview question
Which statement accurately describes the primary function of Dart's null-aware access operator (?. )?
- a.It safely attempts to access a member, returning null if the object itself is null.Correct
- b.It assigns a value to a variable only if that variable is currently null.
- c.It asserts that a value is non-null, causing a runtime error if it is null.
- d.It provides a fallback value if the preceding expression evaluates to null.
Why? this is the answer
The null-aware access operator (?. ) safely checks if an object is null before attempting to access its members. If the object is null, the expression short-circuits and returns null, preventing a runtime error. Option D describes the if-null operator (??), and Option C describes the unsafe not-null assertion operator (!).
Just read this? Test yourself on what you have been reading.
Read the original → dart.dev
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.
See open roles