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.
Add this to your package's pubspec.yaml
file:
dependencies:
firestore_cache: ^2.15.1
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:
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 timestampe 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,
);