How do i know all my changes made to document in offline mode are synced to firestore when i turn on internet in flutter firestore

Issue

I am creating flutter app, where I am using Cloud Firestore as a database and I am utilizing its offline capability. the app perfectly works when I am in offline mode it saves documents locally and when I turn on the internet it syncs those documents back to Firestore.

I want to know is there any way to know that all of my documents are synced when I turn on the internet, like some event listener or something?

I didn’t find anything in the documentation.

Solution

I want to know if is there any way to know that all of my documents are synced when I turn on the internet, like some event listener or something?

Yes, you can know that in Flutter too. So please check the SnapshotMetadata of a QuerySnapshot object, which contains an isFromCache field that:

Whether the snapshot was created from cached data rather than guaranteed up-to-date server data.

If your Flutter project requires you to listen for metadata changes, then please check the official documentation:

If you want to receive snapshot events when the document or query metadata changes, pass a listen to options object when attaching your listener:

final docIdRef = db.collection("collName").doc("docId");
docIdRef.snapshots(includeMetadataChanges: true).listen((event) {
  //Your logic.
});

In this way, you’ll be able to update the UI elements of your screens, once the data is synchronized with the Firebase servers.

Answered By – Alex Mamo

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *