calculateAvarage property

double get calculateAvarage

Calculates and returns the average value based on the key-value pairs in the current Map.

The getter iterates over each key-value pair in the Map. It calculates the sum of the products of each key multiplied by its corresponding value, as well as the total count of values. The average is then computed by dividing the total sum by the count of values.

Returns the average value as a double.

Example:

Map<String, int> data = {
  '10': 3,
  '20': 2,
  '30': 4,
};

double average = data.calculateAverage;
print(average); // Output: 22.5

Implementation

double get calculateAvarage {
  double total = 0;
  double count = 0;

  this.forEach((key, value) {
    total += (double.parse(key) * value);
    count += value;
  });

  return (total / count);
}