FirestoreQueryBuilder<Document> constructor

const FirestoreQueryBuilder<Document>({
  1. Key? key,
  2. required Query<Document> query,
  3. required FirestoreQueryBuilderSnapshotBuilder<Document> builder,
  4. int pageSize = 10,
  5. Widget? child,
})

Listens to a query and paginates the result in a way that is compatible with infinite scroll views, such as ListView or GridView.

FirestoreQueryBuilder will subscribe to the query and obtain the first pageSize items (10 by default). Then as the UI needs to render more items, it is possible to call FirestoreQueryBuilderSnapshot.fetchMore to obtain more items.

FirestoreQueryBuilder is independent from how the query will be rendered and as such can be used with any existing widget for rendering list of items.

An example of how to combine FirestoreQueryBuilder with ListView would be:

FirestoreQueryBuilder<Movie>(
  query: moviesCollection.orderBy('title'),
  builder: (context, snapshot, _) {
    if (snapshot.isFetching) {
      return const CircularProgressIndicator();
    }
    if (snapshot.hasError) {
      return Text('error ${snapshot.error}');
    }

    return ListView.builder(
      itemCount: snapshot.docs.length,
      itemBuilder: (context, index) {
        // if we reached the end of the currently obtained items, we try to
        // obtain more items
        if (snapshot.hasMore && index + 1 == snapshot.docs.length) {
          // Tell FirestoreQueryBuilder to try to obtain more items.
          // It is safe to call this function from within the build method.
          snapshot.fetchMore();
        }

        final movie = snapshot.docs[index];
        return Text(movie.title);
      },
    );
  },
)

Implementation

const FirestoreQueryBuilder({
  Key? key,
  required this.query,
  required this.builder,
  this.pageSize = 10,
  this.child,
})  : assert(pageSize > 1, 'Cannot have a pageSize lower than 1'),
      super(key: key);