3
votes

At the moment I have document snapshots that are coming from my phone's cache. I know this from

print(document.metadata.isFromCache ? "NOT FROM NETWORK" : "FROM NETWORK");// document is a DocumentSnapshot

flutter: NOT FROM NETWORK

However I have deleted some of these documents off firestore, but they are still appearing. I would like to know if there is a way in which I can reset my cache for firestore in flutter so that all the documents are from the cloud.

Thanks for any help - much appreciated.

4
A lot here depends on how you read the documents in question. The document.metadata.isFromCache should typically only be true if the device has no connection to the server, in which case it is impossible for it to know that the document was deleted. If you think that doesn't explain your scenario, you might want to edit the question to include step-by-step instructions and code on how we can reproduce the problem. - Frank van Puffelen

4 Answers

2
votes

It sounds like a bug if a document shows up in a query where the source document has been deleted, if the app has connectivity and can synchronize with Firestore. If you can reproduce this situation with specific steps, the Firestore team might want your bug report.

If you just want to start with a clean cache for development, you can uninstall and reinstall the app to clear the cache.

On Android, there is a system option where you can clear app data (there might be a similar iOS option as well).

1
votes

It can happen when getting a PERMISSION_DENIED response from Firestore, that firestore decides to use the cached version if available.

In my case I was testing with Firestore Emulator, not sure if this will also happen when pointing to production Firestore.

To solve the issue I added persistenceEnabled: false at the moment of setting up Firestore, which in my case lives in main.dart. Setup snippet now looks like this:

   await Firestore.instance.settings(
       host: "localhost:8080",
       sslEnabled: false,
       persistenceEnabled: false,
     );
1
votes

New Flutterfire SDKs behave same as web/android/ios SDKs.

let snapshot = await documentRef.get({ source: 'cache' }
if (!snapshot.exists) {
    snapshot = await documentRef.get({ source: 'server' })
}

The same can be done in new flutter Firestore sdk also, more info--> Doug Stevenson Medium post

0
votes
String host = Platform.isAndroid ? '10.0.2.2:8080' : 'localhost:8080';

  FirebaseFirestore.instance.settings = Settings(
    host: host,
    sslEnabled: false,
    persistenceEnabled: false,
  );