Using Offline Persistence in Firestore in a Flutter App

I think the currrent answer is outdated. According to the firebase documentation, offline persistentence is enabled by default for Android and iOS. For the web, it is not.

In flutter, the firestore implementation is based on the underlying OS. So you're safe on mobile apps, but not with flutter for web.


It is enabled by default but still only when you are not using await or transactions, further you can use timeout to stop listening to network connection by firestore after a specific time.

 ref.setData(newNote.toMap()).timeout(Duration(seconds: 2),onTimeout:() {

        //cancel this call here

        print("do something now");
      });

when you use offline persistence in Firestore, don't use Transactions or await for response.

so, change this :

  await Firestore.instance.collection('certificados').document((certID.toString()))
      .setData(finalDataMap);

to this:

 Firestore.instance.collection('certificados').document((certID.toString()))
      .setData(finalDataMap);

When you restore your internet connection your data will be sync automatically, even if you are in background.

Doesn't work when your app is closed.

Context Of Promises & Callbacks when Offline

Why the above code change to remove "await" works.

Reference: Firebase Video - How Do I Enable Offline Support 11:13

Your callback won't be called and your promise won't complete until the document write has been successful on the server. This is why if your UI waits until the write completes to do something, it appears to freeze in "offline mode" even if the write was actually made to the local cache.

It is OK to not use async / await, .then() or callbacks. Firestore will always "act" as if the data change was applied immediately, so you don't need to wait to be working with fresh data.

You only need to use callbacks and promises when you need to be sure that a server write has happened, and you want to block other things from happening until you get that confirmation.