limitToLast method

Query<T> limitToLast(
  1. int limit
)

Creates and returns a new Query that only returns the last matching documents.

You must specify at least one orderBy clause for limitToLast queries, otherwise an exception will be thrown during execution.

Results for limitToLast queries cannot be streamed.

final query = firestore.collection('col').where('foo', '>', 42);

query.limitToLast(1).get().then((querySnapshot) {
  querySnapshot.forEach((documentSnapshot) {
    print('Last matching document is ${documentSnapshot.ref.path}');
  });
});

Implementation

Query<T> limitToLast(int limit) {
  final options = _queryOptions.copyWith(
    limit: limit,
    limitType: LimitType.last,
  );
  return Query<T>._(
    firestore: firestore,
    queryOptions: options,
  );
}