2

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}?

Sameer Pradhan
  • 25
  • 1
  • 10
  • can you post the specific code? – Ray Coder Jul 11 '20 at 09:44
  • https://stackoverflow.com/a/13611678/13625305 – dev-aentgs Jul 11 '20 at 10:02
  • Does this answer your question ? https://stackoverflow.com/questions/10405348/what-is-the-cleanest-way-to-get-the-sum-of-numbers-in-a-collection-list-in-dart/13611678#13611678 @Sameer Pradhan – void Jul 11 '20 at 10:22
  • 2
    Does this answer your question? [What is the cleanest way to get the sum of numbers in a collection/list in Dart?](https://stackoverflow.com/questions/10405348/what-is-the-cleanest-way-to-get-the-sum-of-numbers-in-a-collection-list-in-dart) – void Jul 11 '20 at 10:22
  • Yes thank you call – Sameer Pradhan Jul 13 '20 at 05:00

3 Answers3

2

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
dev-aentgs
  • 1,218
  • 2
  • 9
  • 17
2
void main() {
  List<int> a=[10,20,30,40];
  int sum=0;
  a.forEach((e){
    sum+=e;
  });
  print(sum);
}

o/p:

100

Abhishek Ghaskata
  • 1,802
  • 2
  • 6
  • 11
1

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() )
Ashraful Haque
  • 1,588
  • 1
  • 13
  • 13