average method

double average()

Returns the average of all elements in the collection.

null elements are counted as 0. Empty collections throw an error.

Implementation

double average() {
  var count = 0;
  num sum = 0;
  for (var current in this) {
    if (current != null) {
      var v = current as num;
      sum += v;
    }
    count++;
  }

  if (count == 0) {
    throw StateError('No elements in collection');
  } else {
    return sum / count;
  }
}