Firestore 缓存

一个 Flutter 插件,用于从缓存首先读取 Firestore 文档,然后再从服务器读取。

此插件主要针对使用 cloud_firestore 插件中的 DocumentReference.get() 和 Query.get() 方法的应用程序,并实现优先从缓存读取,然后从服务器读取。

入门

将此添加到您的包的 pubspec.yaml 文件中

dependencies:
  firestore_cache: ^1.0.0+1

用法

在使用插件之前,您需要先在 Firestore 中创建一个文档,并在该文档中创建一个时间戳字段。请参阅下面的截图作为示例

firestore_screenshot

请注意 此插件不会比较缓存中的文档和服务器上的文档来决定是否应从服务器获取数据。相反,它依靠文档中的时间戳字段来做出该决定。因此,如果希望从服务器读取新数据而不是从缓存读取,您的应用程序应实现更新此字段的逻辑。

您还应该为要获取的不同集合或文档创建不同的时间戳字段。

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

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

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

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

GitHub

https://github.com/zeshuaro/firestore_cache