Skip to content

Paginate Collections

Efra Espada edited this page Feb 22, 2024 · 1 revision

The next method allows you to fetch the results of the next page from your Firestore query. Additionally, it includes a callback parameter noMore which is invoked when there are no more results available.

To use the next method you must include itemsPerPage in your query, this will allow you to paginate documents from your collection.

chatsCollection?.next<Chat>(
  noMore: () {
    refresh();
  },
);

Moreover, the previous method facilitates the retrieval of results from the previous page of results:

chatsCollection?.previous<Chat>();

Ensure that your query includes the itemsPerPage parameter to utilize the previous method effectively.

To enhance query performance, consider including the maxActivePages parameter. This limits the number of active pages in memory, which can help manage resources efficiently.

final chatsCollection = Collection(
  reference: FirebaseFirestore.instance.collection('chats'),
  query: (query) => query
    .orderBy(
      'date',
      descending: true,
    )
    .itemsPerPage(30)
    .maxActivePages(3),
);

Including maxActivePages allows you to control the number of pages kept in memory, optimizing resource usage while still maintaining efficient pagination. This ensures a smoother user experience and better performance for your Firestore queries.