orderByFieldPath method
Creates and returns a new Query that's additionally sorted by the specified field, optionally in descending order instead of ascending.
This function returns a new (immutable) instance of the Query (rather than modify the existing instance) to impose the field mask.
fieldPath
: The field to sort by.descending
(false by default) Whether to obtain documents in descending order.
final query = firestore.collection('col').where('foo', WhereFilter.equal, 42);
query.orderBy('foo', descending: true).get().then((querySnapshot) {
querySnapshot.forEach((documentSnapshot) {
print('Found document at ${documentSnapshot.ref.path}');
});
});
Implementation
Query<T> orderByFieldPath(
FieldPath fieldPath, {
bool descending = false,
}) {
if (_queryOptions.startAt != null || _queryOptions.endAt != null) {
throw ArgumentError(
'Cannot specify an orderBy() constraint after calling '
'startAt(), startAfter(), endBefore() or endAt().',
);
}
final newOrder = _FieldOrder(
fieldPath: fieldPath,
direction: descending ? _Direction.descending : _Direction.ascending,
);
final options = _queryOptions.copyWith(
fieldOrders: [..._queryOptions.fieldOrders, newOrder],
);
return Query<T>._(
firestore: firestore,
queryOptions: options,
);
}