Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i add values together in a user defined object list dart?

List<Product> myProducts = [];

I have made a list builder so that with every product clicked it is added to the list


myProducts.add(Product(
                                  id: product.id,
                                  name: product.name,
                                  price: product.price));

how do i add the price of all the values in the list?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I wasnt sure what to do so I tried researching

myProducts.reduce((value, element) => element.price + value.price)

is this the way to do it?
I hope to get the sum of all the values in my list

>Solution :

double totalPrice = myProducts.fold(0, (sum, product) => sum + product.price);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading