aggregate method

AggregateQuery aggregate(
  1. List<AggregateField> aggregateFields
)

Returns an AggregateQuery that calculates the given aggregations over the result set of this query without downloading the documents.

Throws an ArgumentError if aggregateFields is empty. Like count, the aggregation runs on the server and fails when the client is offline.

final AggregateQuerySnapshot snapshot = await firestore
    .collection('cities')
    .aggregate([
      AggregateField.count(),
      AggregateField.sum('population'),
      AggregateField.average('population'),
    ])
    .get();
print(snapshot.count);
print(snapshot.get(AggregateField.sum('population')));
print(snapshot.getAverage('population'));

Implementation

AggregateQuery aggregate(List<AggregateField> aggregateFields) {
  if (aggregateFields.isEmpty) {
    throw ArgumentError('At least one AggregateField is required.');
  }
  return AggregateQueryInternal.create(this, aggregateFields);
}