Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter - how do calculate average the data in list?

Tags:

list

flutter

dart

I have a List<Comment> about food from users. It looks like this:

[
    {userId: 1,
     rating: 4.5
    },
    {userId: 2,
     rating: 4.0
    },
    {userId: 3,
     rating: 3.5
    },
    {userId: 4,
     rating: 3.0
    }

   ...
];

I want to get the average rating. AVERAGE = Number of ratings : total user, how do I apply this in dart?

like image 724
Muhammad Imanudin Avatar asked Jan 30 '19 13:01

Muhammad Imanudin


1 Answers

  var values = [
    {'userId': 1, 'rating': 4.5},
    {'userId': 2, 'rating': 4.0},
    {'userId': 3, 'rating': 3.5},
    {'userId': 4, 'rating': 3.0}
  ];

  var result = values.map((m) => m['rating']).reduce((a, b) => a + b) / values.length;
  print(result);
like image 181
Günter Zöchbauer Avatar answered Sep 19 '22 16:09

Günter Zöchbauer