Flutter Firestore Set Timeout

Viewed 3486

As far as I know Flutter Firestore operation will keep retry when the internet connectivity is down. Is there a way to set timeout duration so that Firestore throw an error in CatchError when it exceeds the timeout duration?

2 Answers

try this to handle a timeout at app initialization:

  Future<FirebaseApp> app;
  void appInit() {
    app.timeout(Duration(seconds: 5), onTimeout: (){
      // handle app timeout here
    });
    app = FirebaseApp.configure(
      name: 'test',
      options: const FirebaseOptions(
        googleAppID: googleAppID,
        gcmSenderID: projectID,
        apiKey: apiKey,
        projectID: projectID,
      ),
    );
  }

this will handle a transaction timeout:

Firestore.instance.runTransaction((Transaction tx) {
  // handle transaction here
}).timeout(Duration(seconds: 5), onTimeout: () {
  // handle transaction timeout here
});

Firestore Offline mode is enabled by default for Android and IOS!

Cloud Firestore supports offline data persistence. This feature caches a copy of the Cloud Firestore data that your app is actively using, so your app can access the data when the device is offline. You can write, read, listen to, and query the cached data. When the device comes back online, Cloud Firestore synchronizes any local changes made by your app to the Cloud Firestore backend.

For that reason, if you are not dealing with transactions (atomic operations), then You don't need to await for Firestore acks, and you don't need to use timeouts at all!

Important note

Whenever users go online again, their actions will take place immediately, so you want to make sure to use actions such as ArrayUnion on lists and Increment for numeric values.

Web

Offline mode can be manually enabled for Web.

Related