FirebaseDatabaseListView constructor

FirebaseDatabaseListView({
  1. Key? key,
  2. required Query query,
  3. required FirebaseItemBuilder itemBuilder,
  4. int pageSize = 10,
  5. FirebaseLoadingBuilder? loadingBuilder,
  6. FirebaseErrorBuilder? errorBuilder,
  7. Axis scrollDirection = Axis.vertical,
  8. bool reverse = false,
  9. ScrollController? controller,
  10. bool? primary,
  11. ScrollPhysics? physics,
  12. bool shrinkWrap = false,
  13. EdgeInsetsGeometry? padding,
  14. double? itemExtent,
  15. Widget? prototypeItem,
  16. bool addAutomaticKeepAlives = true,
  17. bool addRepaintBoundaries = true,
  18. bool addSemanticIndexes = true,
  19. double? cacheExtent,
  20. int? semanticChildCount,
  21. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  22. ScrollViewKeyboardDismissBehavior keyboardDismissBehavior = ScrollViewKeyboardDismissBehavior.manual,
  23. String? restorationId,
  24. Clip clipBehavior = Clip.hardEdge,
})

A ListView.builder that obtains its items from a Firestore query.

As an example, consider the following collection:

final moviesCollection = FirebaseDatabase.instance.ref('movies');

Using FirebaseDatabaseListView, we can now show the list of movies by writing:

FirebaseDatabaseListView(
  query: moviesCollection,
  itemBuilder: (context, snapshot) {
    var movie = snapshot.value as Map;
    return Text(movie['title']);
  },
)

For advanced UI use-cases, consider switching to FirebaseDatabaseQueryBuilder.

Implementation

FirebaseDatabaseListView({
  Key? key,
  required Query query,
  required FirebaseItemBuilder itemBuilder,
  int pageSize = 10,
  FirebaseLoadingBuilder? loadingBuilder,
  FirebaseErrorBuilder? errorBuilder,
  Axis scrollDirection = Axis.vertical,
  bool reverse = false,
  ScrollController? controller,
  bool? primary,
  ScrollPhysics? physics,
  bool shrinkWrap = false,
  EdgeInsetsGeometry? padding,
  double? itemExtent,
  Widget? prototypeItem,
  bool addAutomaticKeepAlives = true,
  bool addRepaintBoundaries = true,
  bool addSemanticIndexes = true,
  double? cacheExtent,
  int? semanticChildCount,
  DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  ScrollViewKeyboardDismissBehavior keyboardDismissBehavior =
      ScrollViewKeyboardDismissBehavior.manual,
  String? restorationId,
  Clip clipBehavior = Clip.hardEdge,
}) : super(
        key: key,
        query: query,
        pageSize: pageSize,
        builder: (context, snapshot, _) {
          if (snapshot.isFetching) {
            return loadingBuilder?.call(context) ??
                const Center(child: CircularProgressIndicator());
          }

          if (snapshot.hasError && errorBuilder != null) {
            return errorBuilder(
              context,
              snapshot.error!,
              snapshot.stackTrace!,
            );
          }

          return ListView.builder(
            itemCount: snapshot.docs.length,
            itemBuilder: (context, index) {
              final isLastItem = index + 1 == snapshot.docs.length;
              if (isLastItem && snapshot.hasMore) snapshot.fetchMore();

              final doc = snapshot.docs[index];
              return itemBuilder(context, doc);
            },
            scrollDirection: scrollDirection,
            reverse: reverse,
            controller: controller,
            primary: primary,
            physics: physics,
            shrinkWrap: shrinkWrap,
            padding: padding,
            itemExtent: itemExtent,
            prototypeItem: prototypeItem,
            addAutomaticKeepAlives: addAutomaticKeepAlives,
            addRepaintBoundaries: addRepaintBoundaries,
            addSemanticIndexes: addSemanticIndexes,
            cacheExtent: cacheExtent,
            semanticChildCount: semanticChildCount,
            dragStartBehavior: dragStartBehavior,
            keyboardDismissBehavior: keyboardDismissBehavior,
            restorationId: restorationId,
            clipBehavior: clipBehavior,
          );
        },
      );