Sameer Pradhan
Sameer Pradhan

Reputation: 25

How to calculate total sum of data which is double in a list?

Suppose ${d.price} has all the prices (which is a double) of items in the list. How can i calculate the total sum of price using ${d.price}?

Upvotes: 2

Views: 2115

Answers (3)

Ashraful Haque
Ashraful Haque

Reputation: 1698

if d.price has all the prices then an easy approach to calculate the sum is:

int sum = 0;
for(int i=0; i<d.price.length; i++) {
    sum += d.price[i];
}

print( sum );

Updated Code

if you want to show the result you can use Text Widget.

Text( sum.toString() )

Upvotes: 1

Abhishek Ghaskata
Abhishek Ghaskata

Reputation: 1960

void main() {
  List<int> a=[10,20,30,40];
  int sum=0;
  a.forEach((e){
    sum+=e;
  });
  print(sum);
}

o/p:

100

Upvotes: 2

dev-aentgs
dev-aentgs

Reputation: 1288

You can use fold to calculate the sum Reference for example :

void main() {
  List<int> prices = [10,20,30];
  int sum = prices.fold(0, (p, c) => p + c);
  print(sum);
}

Output

60

Upvotes: 3

Related Questions