How do I cleanly map / sort / fold / sort / expand using only cascades or chained calls, in Dart?

dart

Solution

Can you add some parens? I guess something like this...

return ((thing
         .map(...)
         .toList()..sort(...))
           .fold(...)..sort(...))
             .expand(...)
             .toList();

Problem

I would like to perform the following operations on a source list: - map - toList - sort - fold - sort - expand - toList 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?

Original source