?..) 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.
Consider the following example code
var paint = Paint() ..color = Colors.black ..strokeCap = StrokeCap.round ..strokeWidth = 5.0;
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 = 'firstname.lastname@example.org' ..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'.
sb.write() call returns void, and you can’t construct a cascade on
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);