getDocument static method

Future<DocumentSnapshot> getDocument(
  1. DocumentReference docRef, {
  2. Source source = Source.cache,
  3. bool isRefreshEmptyCache = true,
})

Fetch a document with read from cache first then server.

This method takes in a docRef which is the usual DocumentReference object on Firestore used for retrieving a single document. It tries to retrieve the document from the cache first, and fallback to retrieving from the server if it fails to do so. It also takes in an optional argument source which you can force it to fetch the document from the server, an isRefreshEmptyCache to refresh the cached document from the server if it is empty.

This method should only be used if the document you are fetching does not change over time. Once the document is cached, it will always be read from the cache.

Implementation

static Future<DocumentSnapshot> getDocument(
  DocumentReference docRef, {
  Source source = Source.cache,
  bool isRefreshEmptyCache = true,
}) async {
  DocumentSnapshot doc;
  try {
    doc = await docRef.get(GetOptions(source: source));
    if (doc.data() == null && isRefreshEmptyCache) doc = await docRef.get();
  } on FirebaseException {
    // Document cache is unavailable so we fallback to default get document behavior.
    doc = await docRef.get();
  }

  return doc;
}