Add the values in a list
Add every item once. Repeated values still count each time they occur.
Iterable.fold
Simple example
Add one, two and three.
Start1 · 2 · 3
Result6
void main() {
final values = <int>[1, 2, 3];
print(values.fold<int>(0, (total, value) => total + value));
}
6
Advertisements
Everyday examples
Add the three amounts on a receipt.
Start5 · 5 · 8
Result18
void main() {
final values = <int>[5, 5, 8];
print(values.fold<int>(0, (total, value) => total + value));
}
18
How many passengers boarded at the three stops?
Start2 · 7 · 1
Result10
void main() {
final values = <int>[2, 7, 1];
print(values.fold<int>(0, (total, value) => total + value));
}
10
How many exercise books arrived in three boxes?
Start10 · 8 · 9
Result27
void main() {
final values = <int>[10, 8, 9];
print(values.fold<int>(0, (total, value) => total + value));
}
27