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

插件详情v2.21.7

firestore_cache

一个用于从缓存中优先读取,然后从服务器获取 Firestore 文档的 Flutter 插件

AndroidiOSWebmacOSWindows
近 30 天下载
3340
喜欢数
107
Pub 分数
160

安装插件

flutter pub add firestore_cache

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

firestore_cache 是一个 Flutter 包,可实现从缓存优先读取 Firestore 文档的策略,确保快速的本地响应,同时可选择性地与服务器同步。它优先使用缓存数据以实现即时显示,并异步从服务器更新缓存,从而在网络延迟或离线情况下提升应用性能和用户体验。该包支持所有主要平台,包括 Android、iOS、Web、macOS 和 Windows,非常适合需要响应式数据访问并具备备用可靠性的应用程序。

适合场景

  • 离线优先的数据检索
  • 快速的初始UI渲染
  • 与 Firestore 的后台同步
  • 改进的应用响应速度
  • 无缝网络恢复

主要特点

  • 缓存优先文档获取
  • 自动服务器同步
  • 跨平台支持
  • 简单 API 集成
  • 实时数据一致性

适合对象

  • 需要快速本地数据访问的应用程序
  • 开发人员实现离线功能
  • 使用 Firebase Firestore 的项目
  • 需要即时反馈的UI
  • 网络连接不可靠的应用程序

使用建议

  • 最初可能提供过时的数据
  • 需要仔细处理缓存失效
  • 不适用于实时关键更新
  • 限制为 Firestore 文档读取
  • 网络使用量取决于同步频率

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Firestore Cache

pub package docs MIT License GitHub Actions codecov Codacy Badge style: flutter_lints

Github-sponsors BuyMeACoffee Ko-Fi LiberaPay Patreon PayPal

A Flutter plugin for fetching Firestore documents with read from cache first then server.

This plugin is mainly designed for applications using the DocumentReference.get() and Query.get() methods in the cloud_firestore plugin, and is implemented with read from cache first then server.

Getting Started

Add this to your package's pubspec.yaml file:

dependencies:
  firestore_cache: ^2.21.2
Usage

Before using the plugin, you will need to create a document on Firestore and create a timestamp field in that document. See the screenshot below for an example:

Firestore Screenshot

⚠️ PLEASE NOTE This plugin does not compare the documents in the cache and the ones in the server to determine if it should fetch data from the server. Instead, it relies on the timestamp field in the document to make that decision. And so your application should implement the logic to update this field if you want to read new data from the server instead of reading it from the cache.

You should also create different timestamp fields for different collections or documents that you are fetching.

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

// This should be the path of the document containing the timestamp field
// that you created
final cacheDocRef = Firestore.instance.doc('status/status');

// This should be the timestamp field in that document
final cacheField = 'updatedAt';

final query = Firestore.instance.collection('posts');
final snapshot = await FirestoreCache.getDocuments(
    query: query,
    cacheDocRef: cacheDocRef,
    firestoreCacheField: cacheField,
);

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目

暂无相关数据。