1

I do a query like this:

  var snapshots = await this.db
      .collection('${AppInit.chatsref}')
      .where('members', arrayContains: uid)
      .orderBy('updated_at', descending: true)
      .get();
  // Check cache.
    print(snapshots.metadata.isFromCache ? "Cached" : "Not Cached");

I will always get the 'Not cached' So my cache only works if I specifically set source to cache

  var snapshots = await this.db
              .collection('${AppInit.chatsref}')
              .where('members', arrayContains: uid)
              .orderBy('updated_at', descending: true)
              .get(GetOptions(source: Source.cache)); 
    // Check cache.
    print(snapshots.metadata.isFromCache ? "Cached" : "Not Cached");

Then I will get content from the cache. I thought firebase firestore handles this. Do I need to always set the source? This happens for both the firestore future(get) and stream(snapshot).

I want firestore to use cache as much as possible. It's a chatting app. So I hate that the app stays at the loading state even for a second: enter image description here

LearnToday
  • 2,762
  • 9
  • 38
  • 67
  • 2
    "The isFromCache flag indicates whether the document is guaranteed to be up to date with the server, or that it may be a stale result from the cache. If it's false that doesn't necessarily means the document was read from the server, but that you can be guaranteed that the document is up to date with the server." See https://stackoverflow.com/questions/59627868/firebase-always-reads-from-internet/59637177#59637177 So you shouldn't have to specify a source. But you should use a snapshot listener instead of `get()`, to ensure it shows the local data right away. – Frank van Puffelen May 19 '21 at 13:12

0 Answers0