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}
?
Asked
Active
Viewed 1,648 times
2

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
-
2Does 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 Answers
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
-
-
It is hard for me to tell where should you use this code by not seeing your code. If you provide more details then maybe I can tell you. Thank you – Ashraful Haque Jul 13 '20 at 06:33
-
its just that i have to show the total sum in a text form. any alternative for that? – Sameer Pradhan Jul 13 '20 at 09:31
-
you can use a text widget to show the result. I have updated the code. You can have a look to get an idea about it. – Ashraful Haque Jul 13 '20 at 16:37
-