Dart Iterable fold()

Dart Iterable fold()

·

1 min read

fold():-

  • Reduces a collection to a single value by iteratively combining each element of the collection with an existing value

  • Uses initialValue as the initial value, then iterates through the elements and updates the value with each element using the combine function.

Example:

void main() {
  var nums = [1, 2, 3, 4, 5];
  int sum = nums.fold(0, (previousValue, element) => previousValue + element);
  print(sum);
}
15

Exited.