I'm trying to understand when Firestore caches something. To do so, I run the following code: I would expect the first get to have isFromCache
false and I would expect that the second get would come from cache.
FirebaseFirestore.instance.collection('questions').doc("xyz").get().then((DocumentSnapshot documentSnapshot) {
print("1st: " + documentSnapshot.metadata.isFromCache.toString());
FirebaseFirestore.instance.collection('questions').doc("xyz").get(GetOptions(source: Source.cache)).then((DocumentSnapshot documentSnapshot) {
print("2nd: " + documentSnapshot.metadata.isFromCache.toString());
});
});
However, when running this (dart, running in Chrome) I noticed the following logs so apparently, the document is not cached. Is this expected behavior? Am I missing something?
1st: false
zone.dart:1339 Uncaught [cloud_firestore/unavailable] Failed to get document from cache. (However, this document may exist on the server. Run again without setting 'source' in the GetOptions to attempt to retrieve the document from the server.)
at Object.wrapException (http://localhost:41665/main.dart.js:2047:17)
at Object.throwExpression (http://localhost:41665/main.dart.js:2061:15)
at guard_closure0.call$1 (http://localhost:41665/main.dart.js:22924:16)
at _RootZone.runUnary$2$2 (http://localhost:41665/main.dart.js:16321:18)
at _RootZone.runUnary$2 (http://localhost:41665/main.dart.js:16325:19)
at _FutureListener.handleError$1 (http://localhost:41665/main.dart.js:15447:19)
at _Future__propagateToListeners_handleError.call$0 (http://localhost:41665/main.dart.js:15732:49)
at Object._Future__propagateToListeners (http://localhost:41665/main.dart.js:5177:77)
at _Future._completeError$2 (http://localhost:41665/main.dart.js:15577:9)
at _Future__asyncCompleteError_closure.call$0 (http://localhost:41665/main.dart.js:15663:18)
EDIT: when adding a listener like below, I do get "1st: false, 2nd: true" as expected.
FirebaseFirestore.instance.collection('questions').doc("xyx").snapshots().listen((event)
{
print("update received");
});
FirebaseFirestore.instance.collection('questions').doc("xyz").get().then((DocumentSnapshot documentSnapshot) {
print("1st: " + documentSnapshot.metadata.isFromCache.toString());
FirebaseFirestore.instance.collection('questions').doc("xyz").get(GetOptions(source: Source.cache)).then((DocumentSnapshot documentSnapshot) {
print("2nd: " + documentSnapshot.metadata.isFromCache.toString());
});
});