sumBy
Sum a key of every element — map + sum in one step, 0 when empty.
Lecture
sumBy collapses the most common two-stage tail in data
pipelines — .map((x) => x.field).sum() — into a single
terminal. It folds a running total while extracting the key, so nothing
intermediate is materialized and the intent ("total this field") is one
call instead of a projection plus an aggregation.
The contract is sum's: an empty
pipeline totals to 0, and int keys keep int arithmetic
while any double key promotes the total to double.
This is a Dart-native addition (FxTS ships only the numeric
sum), in the same family as
maxBy /
minBy — Kotlin spells it
sumOf.
Demo 1 · Basics & the empty case
Demo 2 · Async keys
Try it yourself
Exercise: total the long words' lengths with one call.