Null-Shorting Cascades (?..)
Cascade safely on nullable objects.
Null-Shorting Cascades (?..) is a free Dart Academy lesson on CoddyKit — lesson 2 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.
The Nullable Trap
A plain cascade on a value that might be null will crash. Calling a method on null throws an error before any line runs.
Meet Null-Shorting
Dart gives you a safe version: the null-shorting cascade, written as question mark then two dots. It only runs when the object is not null.
button?..text = 'Save'
..onTap = save;How the Short Works
If the target is null, the whole cascade is skipped and the expression simply becomes null. No crash, no half-done work.
Only the First Dot Needs It
You write the question mark just once, on the first cascade. The later lines stay plain double dots; Dart already knows the object is non-null.
obj?..a()
..b()
..c();All or Nothing
Null-shorting is all or nothing: either every line runs because the object exists, or none of them do. The chain never runs halfway.
Compared to Plain Cascade
A plain cascade assumes the object is there. The null-shorting form is your safety net when the type is nullable and might be missing.
It Returns Nullable
Since the result can be the object or null, the whole expression has a nullable type. Treat what you get back as possibly null.
A Real Example
Imagine a config that may not exist yet. Null-shorting lets you set it up only when it is present, in one clean line. 🛡️
settings?..theme = dark
..save();Don't Repeat the Mark
Writing question mark on every line is wrong and Dart flags it. The shorting decision happens once, right at the start of the chain.
Pairs Well With Null Safety
Null-shorting fits Dart's null safety story. The compiler trusts that lines after the first dot run only on a real, present object.
When to Reach for It
Use null-shorting when a variable is nullable and you want to configure it without an extra if-null check around the cascade.
Quick Check
How do you write a null-shorting cascade correctly?
Recap
The null-shorting cascade (?..) runs the whole chain only when the object exists, skips everything when null, and needs the mark just once. 🎯
Frequently asked questions
Is the “Null-Shorting Cascades (?..)” lesson free?
Yes — the full text of “Null-Shorting Cascades (?..)” 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-Shorting Cascades (?..)”?
Cascade safely on nullable objects. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Null-Shorting Cascades (?..)” 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
- The Cascade Operator (..)
- Null-Shorting Cascades (?..)
- Spread and Null-Aware Spread
- Collection if and for