Cascade operator (.., ?.. ) and Triple dot […] (Spread Operator) in Dart.
Cascades (..
, ?..
) allow you to make a sequence of operations on the same object. In addition to accessing instance members, you can also call instance methods on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.
Example
Consider the following example code
var paint = Paint()
..color = Colors.black
..strokeCap = StrokeCap.round
..strokeWidth = 5.0;
The constructor, Paint()
, returns a Paint
object. The code that follows the cascade notation operates on this object, ignoring any values that might be returned.
The previous example is equivalent to this code:
var paint = Paint();
paint.color = Colors.black;
paint.strokeCap = StrokeCap.round;
paint.strokeWidth = 5.0;
If the object that the cascade operates on can be null, then use a null-shorting cascade (?..
) for the first operation. Starting with ?..
guarantees that none of the cascade operations are attempted on that null object.
querySelector('#confirm') // Get an object.
?..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'))
..scrollIntoView();
The previous code is equivalent to the following:
final addressBook = (AddressBookBuilder()
..name = 'jenny'
..email = 'jenny@example.com'
..phone = (PhoneNumberBuilder()
..number = '415-555-0100'
..label = 'home')
.build())
.build();
Be careful to construct your cascade on a function that returns an actual object. For example, the following code fails:
var sb = StringBuffer();
sb.write('foo')
..write('bar'); // Error: method 'write' isn't defined for 'void'.
The sb.write()
call returns void, and you can’t construct a cascade on void
.
Spread Operator
Dart 2.3 introduced the spread operator (...
) and the null-aware spread operator (...?
), which provide a concise way to insert multiple values into a collection.
For example, you can use the spread operator (...
) to insert all the values of a list into another list:
var list = [1, 2, 3];
var list2 = [0, ...list];
assert(list2.length == 4);
If the expression to the right of the spread operator might be null, you can avoid exceptions by using a null-aware spread operator (...?
):
var list2 = [0, ...?list];
assert(list2.length == 1);
Reference
https://dart.dev/guides/language/language-tour#_operators
https://dart.dev/guides/language/language-tour#spread-operator