buildQuery function

Query buildQuery({
  1. required Query collection,
  2. List<QueryConstraint>? constraints,
  3. List<OrderConstraint>? orderBy,
})

Builds a query dynamically based on a list of QueryConstraint and orders the result based on a list of OrderConstraint. collection : the source collection for the new query constraints : a list of constraints that should be applied to the collection. orderBy : a list of order constraints that should be applied to the collection after the filtering by constraints was done. Important all limitation of FireStore apply for this method two on how you can query fields in collections and order them.

Implementation

Query buildQuery({required Query collection, List<QueryConstraint>? constraints, List<OrderConstraint>? orderBy}) {
  Query ref = collection;

  if (constraints != null) {
    for (var constraint in constraints) {
      ref = ref.where(constraint.field,
          isEqualTo: constraint.isEqualTo,
          isGreaterThan: constraint.isGreaterThan,
          isGreaterThanOrEqualTo: constraint.isGreaterThanOrEqualTo,
          isLessThan: constraint.isLessThan,
          isLessThanOrEqualTo: constraint.isLessThanOrEqualTo,
          isNull: constraint.isNull,
          arrayContains: constraint.arrayContains);
    }
  }
  if (orderBy != null) {
    for (var order in orderBy) {
      ref = ref.orderBy(order.field, descending: order.descending);
    }
  }
  return ref;
}