FirebaseDatabaseQueryBuilder constructor

const FirebaseDatabaseQueryBuilder({
  1. Key? key,
  2. required Query query,
  3. required FirebaseQueryBuilderSnapshotBuilder 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.

FirebaseDatabaseQueryBuilder 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 FirebaseQueryBuilderSnapshot.fetchMore to obtain more items.

FirebaseDatabaseQueryBuilder 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 FirebaseDatabaseQueryBuilder with ListView would be:

FirebaseDatabaseQueryBuilder(
  query: moviesCollection),
  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 FirebaseDatabaseQueryBuilder 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].value as Map;
        return Text(movie['title']);
      },
    );
  },
)

Implementation

const FirebaseDatabaseQueryBuilder({
  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);