I got this list in flutter:
List<dynamic> days = [];
Printing it gives me this result:
[[1, 3, 4, 6], [1, 2, 3]]
Now I want to get total items in this list.
What is the best way to do that?
I have tried this but it doesn’t work:
print(days.sum);
I want to get the total items from all lists.
(1,3,4,6 + 1,2,3)= total:7items.
Is that possible?
4
Answers
Considering that you are using
List<dynamic>
as the type ofdays
you can writeIf
days
was declared asList<List>
to begin with likeyou can shorten it to
flattened is part of the
collection
package so you would need to add that and import it like:If you do not wish to use that package you could copy the implementation of it yourself and add it to your code:
You can try to use fold method.
in print it returns 16.
try this
Here is a recursive function. This example prints "8".