Dart cascades
—
Dart
Cascades (..
) allow for performing a sequence of operations on the same object.
The following code
Cart updateCart(Cart cart, List<Product> products, Discount discount) {
var updatedCart = cart.adding(products);
return updatedCart.applying(discount);
}
becomes
Cart updateCart(Cart cart, List<Product> products, Discount discount) {
return cart.adding(products)
..applying(discount);
}
or adding elements to a List
var finalList = someVariable.toList();
finalList.add(anotherObject);
finalList.addAll(anotherListOfObjects);
return finalList
becomes
return someVariable.toList()
..add(anotherObject)
..addAll(anotherListOfObjects);
At first I found them hard to read. But after using them a couple of times, I got used to them.
Leave a comment