Flutter Firestore 设置超时

Zan*_*ane 2 firebase flutter google-cloud-firestore

据我所知,当互联网连接中断时,Flutter Firestore 操作将继续重试。有没有办法设置超时持续时间,以便 Firestore 在超过超时持续时间时在 CatchError 中抛出错误?

Sou*_*tes 5

试试这个来处理应用程序初始化时的超时:

  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,
      ),
    );
  }
Run Code Online (Sandbox Code Playgroud)

这将处理事务超时:

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

  • @kirill_igum “timeout”方法是 Dart 中“Future”类的一部分。您可以在此处找到该文档 https://api.dart.dev/stable/2.10.4/dart-async/Future/timeout.html。 (3认同)