Search code examples
flutterfirebasegoogle-cloud-firestoreoffline

How to force write locally ONLY even when connected to the internet? Firestore Firebase


I am making a register application that has 1000+ units per register. I am making changes to a unit one at a time. I don't want those changes to be written to Firebase immediately.

Instead, I want to prompt the user to manually update the documents once a day. (Internet is expensive in my country.)

So the user will have an internet connection but the changes should reflect locally only with a 'queued to upload' tag.

Can this be achieved?

I am using Dart together with Flutter for Android

CollectionReference<Map<String, dynamic>> collection =
    instance.collection('tasks');

collection.doc('docID').update({data});

Solution

  • You can disable network access for the Firestore client by calling disableNetwork():

    FirebaseFirestore.instance.disableNetwork();
    

    Then call enableNetwork() to re-enable its network access and sync any pending writes (and to synchronize any active listeners).

    Also see the Firebase documentation on disabling and enabling network access.