Sum of Array Element in flutter Dart
Sum of Array Element in flutter Dart.

In this Flutter dart article, Let’s go through different way through which we can calculate sum of array element. In dart array are called as list, let’s checkout how to perform addition of all the elements in a given dart list.

There are many ways by which we can get sum of array element in dart i.e by using normal loop like for loop or by using dart inbuild functions.

Sum of list using dart for loop

void main(){
  final numbers = [1,4,2,3,6,4,2,1,4,5,0];
  
  var sum = 0;
  
  for(var i=0;i<numbers.length;i++){
    sum += numbers[i];
  }
  print(sum);
}

Sum addition of array element using dart forEach loop

void main(){
  final numbers = [3,4,5,1,2,4,2,1,4,7,0];
  
  var sum = 0;
  
  numbers.forEach((val){
      sum += val;
  });
  print(sum);
}

Sum of array list using reduce function

void main(){
  final numbers = [3,4,5,1,2,4,2,1,4,7,0];
 
  var sum = numbers.reduce((a,b)=>a+b);
  
  print(sum);
}

Addition of all element in dart list using collection package sum function

import 'package:collection/collection.dart';

void main() {
  final numbers = [1, 150, 10, 134, 5, 8];
  var sum = numbers.sum;
  print(sum);
}