Published Mar 6, 2024
Contribute to Docs

The .fold() method in Dart is a built-in List method that starts with an initial value and applies a given function to each element of a list to return a single, accumulated value. It’s particularly handy for tasks like calculating aggregate values (e.g., sum, product), concatenating strings, finding maximum or minimum values, generating data structures, and handling errors.


T fold<T>(T initialValue, T combine(previousValue, element))
  • initialValue: The initial value to use for the accumulator.
  • combine: A function that takes the initialValue for the first element and the current element as arguments and returns the new accumulated value.


In this example, the .fold() method is used to calculate the sum of all the elements in the ages list:

void main() {
List<int> ages = [22, 28, 27, 19, 18];
int totalAge = ages.fold(10, (totalLength, element) => totalLength + element);

Here is the output for the above example:


All contributors

Looking to contribute?

Learn Dart on Codecademy