I would like to perform the following operations on a source list:
Some of these methods, like map and toList are chainable, because they return a non-null object. However, the sort
method returns void
as it operates on a List and does not return a list.
Dart has a method cascade operator, which works great for method calls that do not return this
or another object.
I'm having a hard time writing the above sequence of calls without resorting to assigning a variable after the sort
and then starting the chain of calls again.
I'd like to write the equivalent of:
return thing
.map(...)
.toList()
.sort(...)
.fold(...)
.sort(...)
.expand(...)
.toList();
The closest I can get is:
var thing
.map(...)
.toList()
..sort(...);
var otherThing = thing
.fold(...)
..sort(...);
var finalThing = otherThing.expand(...).toList();
Is there a better way? How do I "break out" of the cascade and continue on a chain?
Can you add some parens? I guess something like this...
return ((thing
.map(...)
.toList()..sort(...))
.fold(...)..sort(...))
.expand(...)
.toList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With