Null-Aware Operators: ?., ??, and !
Read and default null values safely.
Null-Aware Operators: ?., ??, and ! is a free Dart Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Dart Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Working With Maybe-Null
Nullable values need gentle handling. Dart's null-aware operators let you read and default them in one short step. ✨
The Safe-Call Operator
Use ?. to call a member only when the object is not null. If it is null, the whole expression becomes null.
String? name;
int? len = name?.length;No Crash on Null
With ?. a null object simply short-circuits instead of throwing. Your program keeps running calmly.
print(name?.toUpperCase());The If-Null Operator
The ?? operator supplies a fallback. It returns the left value, or the right one when the left is null.
String label = name ?? 'Guest';Chaining the Two
Combine them: read safely with ?. then default with ?? so you always end up with a usable value.
int len = name?.length ?? 0;Assign If Null
The ??= operator assigns only when the variable is currently null, a handy way to set defaults once.
name ??= 'Anonymous';The Bang Operator
The ! operator asserts a value is not null right now, converting a nullable type to its plain form.
int sure = name!.length;Bang Is a Promise
Using ! means you take responsibility. If the value really is null, it throws at runtime, so use it sparingly.
Prefer Checks Over Bang
When unsure, an explicit if test or ?? default is safer than !. Reach for bang only when certain.
Reading Nested Data
The safe-call ?. can chain through several links, stopping at the first null without errors.
var city = user?.address?.city;Pick the Right Tool
Use ?. to read safely, ?? to default, and ! only when you can prove a value exists.
Quick Check
Which operator provides a fallback when a value is null?
Recap
You mastered null-aware tools: ?. reads safely, ?? supplies defaults, and ! asserts non-null when you are sure. 🎯
Frequently asked questions
Is the “Null-Aware Operators: ?., ??, and !” lesson free?
Yes — the full text of “Null-Aware Operators: ?., ??, and !” is free to read here on the web, and the Dart Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Dart Academy course, upgrade to CoddyKit PRO.
What will I learn in “Null-Aware Operators: ?., ??, and !”?
Read and default null values safely. You practise Dart Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Dart Academy?
No prior experience is required. Dart Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Null-Aware Operators: ?., ??, and !” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Dart Academy lesson?
Yes. Every Dart Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrow Syntax for One-Line Functions
- Anonymous Functions and Closures
- Nullable Types and the Question Mark
- Null-Aware Operators: ?., ??, and !