'I want to calculate each number from the array in flutter

I have an array in the flutter

 final list = [1, 2, 3, 4, 5];

I want to loop through the array to calculate the sum of all the items in the array,how i can?



Solution 1:[1]

Try the following:

 final list = [1, 2, 3, 4, 5];

 final result = list.reduce((sum, element){
 return sum + element;
 }); 

Solution 2:[2]

This could do it

final sum = list.fold<int>(0, (previousValue, number) => previousValue + number);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Obsidianlab
Solution 2 Ivo Beckers