Here i have multiple nested class data and i want to get sum of SelectedProductServices.count

my data class started with OrderStructure which that have a list as SelectedProducts and each of them have SelectedProductServices which count is here. now how can i use fold or reduce to get sum of them?

for example:>,(a,b)=>a!+b))

should return 600

my code:

void main() {
     OrderStructure order = OrderStructure('test',<SelectedProducts>[
          1, <SelectedProductServices>[
        const SelectedProductServices(1, 100, 1),
        const SelectedProductServices(1, 500, 1),


void getSum(){

class OrderStructure {
  final String title;
  final List<SelectedProducts> products;

  const OrderStructure(this.title, this.products);

class SelectedProducts {
  final int id;
  final List<SelectedProductServices> services;

  bool operator ==(other) => other is SelectedProducts && == id;

  int get hashCode => id.hashCode;

class SelectedProductServices {
  final int id;
  final int count;
  final int cost;

  const SelectedProductServices(, this.count, this.cost);

Solution 1: t00n

This should do it

int getSum(){
     return =>, (int a, b) => a + b.count)).first;

Solution 2: TmKVU

Since you have nested list, you'd have to fold twice to get a single value:

int getSum(){
   int sum(int a, SelectedProductServices s) => a + s.count;
   return =>, sum)).fold(0, (a,b) => a + b);