搜索插件、代码仓与 APP 分类

GitHub 代码仓StageCoding/firestore_paging_controller

firestore_paging_controller

StageCoding/firestore_paging_controller open-source repository details.

firestore_paging_controller 项目封面
星标
4
复刻
1
最近推送(UTC)
2025年1月2日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

Firestore Paging Controller

This is a simple Firestore Paging Controller package build on top of the infinite_scroll_pagination package. It provides a simple way to paginate through Firestore collections.

Additionally, it provides a simple way to merge multiple queries into a single stream, which ultimately allows you to have an OR query. On top of that, it also allows ordering the results of the merged queries, which is not possible with Firestore. Internally, it uses the buffer to make sure it doesn't show the items in the wrong order.

Getting Started

To use this package, you need to add the following dependency in your pubspec.yaml file:

dependencies:
  firestore_paging_controller: any

Usage

To use this package, you need to create a FirestorePagingController and (optionally) pass it the queries you want to paginate through. Here is an example:

import 'package:firestore_paging_controller/firestore_paging_controller.dart';

final controller = FirestorePagingController.withoutType(
  basePath: 'posts',
  queryBuilders: [
    (query) => query.where('creatorId', isEqualTo: userId),
    (query) => query.where('taggedUserId', isEqualTo: userId),
  ],
  orderBy: 'createdAt',
  orderByDescending: true,
  pageSize: 10,
);

This will create a controller that will paginate through the items that have the creatorId or taggedUserId equal to the userId. The controller will fetch 10 items per page for each query.

After creating the controller, you can use it in a widget from the infinite_scroll_pagination package. Here is an example:

PagedListView(
  pagingController: controller,
  builderDelegate: PagedChildBuilderDelegate<QueryDocumentSnapshot<Map<String, dynamic>>>(
    itemBuilder: (context, snapshot, index) => Post(snapshot.data()),
  ),
);

If you want to use the type-safe version of the controller, you can do the following:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firestore_paging_controller/firestore_paging_controller.dart';

final controller = FirestorePagingController<PostModel>.converted(
  basePath: 'posts',
  queryBuilders: [
    (query) => query.where('creatorId', isEqualTo: userId),
    (query) => query.where('taggedUserId', isEqualTo: userId),
  ],
  orderBy: 'createdAt',
  orderByDescending: true,
  pageSize: 10,
  fromMap: (data) => PostModel.fromMap(data),
);

Then it can be used like this:

PagedListView<int, QueryDocumentSnapshot<T>>(
  pagingController: controller,
  builderDelegate: PagedChildBuilderDelegate<QueryDocumentSnapshot<T>>(
    itemBuilder: (context, snapshot, index) => Post(snapshot.data()),
  ),
);

Reminders:

  • The queries you pass to the controller should not have any limit or orderBy applied to them.
  • You may need to create an index in Firestore for the queries you are using in the controller. Look at the logs for the error message and create the index accordingly.
Stage Coding GitHub avatar
GitHub 作者

Stage Coding ↗

Bosnia and Herzegovina官方网站 ↗
语言DartC++CMakeSwiftRubyCHTMLKotlinObjective-C

此仓库发布的插件

使用的依赖

依赖清单 10 项

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗