On this page12 sections
Lecture 029: Task: Cascade Notation
| Day | 10: Functions & Parameters |
| Lecture | 029 of 057 |
| Estimated video length | 10 min |
| Target | Dart 3.12 |
| Level | Beginner |
Setting up an object often means writing its name over and over:
p.name = ...,p.age = ...,p.city = .... The cascade operator..lets you do many things to the same object in one flowing statement. Flutter code uses it a lot.
Contents
Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways
Prerequisites
Lectures 011 (lists) and 025 (functions and methods). The examples use a tiny class; classes are
explained properly on Day 11, so for now just read class Person as "a type with some fields".
Learning Objectives
- Chain several operations on one object with
.. - Set fields and call methods in a cascade
- Know that a cascade gives back the object, not the method's result
- Use the null-aware cascade
?..
1. The Problem
class Person {
String name = '';
int age = 0;
String city = '';
void introduce() {
print('I am $name, $age, from $city');
}
}
var p1 = Person();
p1.name = 'Ali';
p1.age = 25;
p1.city = 'Lahore';
p1.introduce(); // I am Ali, 25, from Lahorep1. is repeated four times.
2. The Cascade
.. means "and then, on the same object…":
var p2 = Person()
..name = 'Sara'
..age = 22
..city = 'Karachi'
..introduce();
print(p2.name); // SaraOutput
I am Sara, 22, from Karachi
SaraEverything after Person() is applied to that one new object, and p2 gets the object itself.
3. Cascades on Lists and Other Objects
var list = <int>[]
..add(3)
..add(1)
..add(2)
..sort();
print(list); // [1, 2, 3]
var buffer = StringBuffer()
..write('Hello')
..write(', ')
..write('World');
print(buffer.toString()); // Hello, World
var sorted = [5, 1, 4]..sort();
print(sorted); // [1, 4, 5]4. The Key Rule: You Get the Object Back
list.sort() returns nothing (void). But list..sort() returns the list:
| Expression | Result |
|---|---|
[5, 1, 4].sort() | nothing (void) |
[5, 1, 4]..sort() | the sorted list |
That's why var sorted = [5, 1, 4]..sort(); works and var sorted = [5, 1, 4].sort(); doesn't
(see Mistakes).
5. The Null-Aware Cascade ?..
If the object might be null, start with ?... The whole cascade is skipped when it's null:
List<int>? maybe;
maybe?..add(1)..add(2);
print(maybe); // null: nothing ran, no crashCode Examples
Example 1: Building an Order
class Order {
String customer = '';
final List<String> items = [];
double discount = 0;
void addItem(String item) => items.add(item);
void summary() {
print('Order for $customer: ${items.join(', ')} (discount ${(discount * 100).round()}%)');
}
}
void main() {
Order()
..customer = 'Bilal'
..addItem('Burger')
..addItem('Fries')
..addItem('Cola')
..discount = 0.1
..summary();
}Output
Order for Bilal: Burger, Fries, Cola (discount 10%)One statement creates the order, fills it in and prints it, without needing a variable at all.
Common Beginner Mistakes
1. Using . where you meant ..
var list = [3, 1, 2].sort();
print(list);Error: This expression has type 'void' and can't be used..sort() gives back nothing. Write [3, 1, 2]..sort().
2. Starting a cascade on a void result
var list = [3, 1, 2].sort()..add(4);Error: This expression has type 'void' and can't be used.
Error: The method 'add' isn't defined for the type 'void'.The cascade must start on the object: [3, 1, 2]..sort()..add(4).
3. A ; in the middle: each .. line is part of one statement. Only the last line ends
with ;.
Practice Questions
- What does
..do? - What is
xaftervar x = [3, 2, 1]..sort();? - Rewrite with a cascade:
var b = StringBuffer(); b.write('A'); b.write('B'); - Why does
var x = [3, 2, 1].sort();fail to compile when you usex? - What happens with
list?..add(1)whenlistisnull?
Practice Answers
- It performs an operation on the same object and gives back that object, so you can chain more operations.
[1, 2, 3]var b = StringBuffer()..write('A')..write('B');sort()returnsvoid, soxwould hold nothing usable.- Nothing. The cascade is skipped, with no crash.
Key Takeaways
object..a()..b = 1..c()does several things to the same object.- A cascade gives back the object, not the method's result.
[...]..sort()gives the sorted list;[...].sort()gives nothing.?..skips the cascade when the object isnull.- One cascade = one statement; only the end gets
;.