Cascade notation (..)
Cascades (..
) allow you to make a sequence of operationson the same object. In addition to function calls,you can also access fields on that same object.This often saves you the step of creating a temporary variable andallows you to write more fluid code.
Consider the following code:
querySelector('#confirm') // Get an object.
..text = 'Confirm' // Use its members.
..classes.add('important')
..onClick.listen((e) => window.alert('Confirmed!'));
The first method call, querySelector()
, returns a selector object.The code that follows the cascade notation operateson this selector object, ignoring any subsequent values thatmight be returned.
The previous example is equivalent to:
var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) => window.alert('Confirmed!'));
You can also nest your cascades. For example:
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 returnsan 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
.
Note: Strictly speaking, the “double dot” notation for cascades is not an operator. It’s just part of the Dart syntax.