'How to calculate average and create a new list out of a existing list in flutter

I have a future which returns a list of 4 items;

  1. Name
  2. Type
  3. Ratings
  4. number of ratings

i want to create a new list out of this list with only Average Ratings and number of ratings.

I want to add them in a new firestore collection.

Please note i want to calculate average for the result of each call so i am avoiding a cloud function because that will calculate average of all the values in existing collection. So flow is, app calls a cloud function, get values and then it calculate average of all the ratings in that call's result.

My future is;

 Future<List<allItems>> createList() async {

HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('ratingClass');
final results = await callable;
final datList = await results.call(<String, dynamic>{
  'filename': fileName,

});
var resultData = await datList.data;

var jList = json.decode(abcdef);

for (var data in jList){
  originalList.add(new allItems( data['name'],data['type'], data['rating'].toString(), data['numberofrating'].toString()));
}


for (var ddata in jList!){
  if (ddata != null) {
    myCollection.add({
      'name': def['name'],
      'type':def['type'],
      'rating':def['ratings'],
      'numberofrating':def['numberofrating'],

    });
  }}

return originalList;

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source