如何在Flutter中为Firebase连接设置侦听器?

时间:2020-10-08 08:28:36

标签: firebase flutter google-cloud-firestore

所以我正在尝试捕获“无网络”错误。

我正在这样在Firestore中听文档:

final docRef = FirebaseFirestore.instance.collection(kInfoCollection).doc(kInfoDocument);

docSubscription = docRef.snapshots().distinct().listen((_) {});

docSubscription.onData((snapshot) => _processInfo(snapshot))

docSubscription.onError((e, s) {
        final errorMessage = 'Message:\n$e \nThe Stack was:\n$s';
        print('Error $errorMessage');
        throw InfoException(errorMessage);
      });

如您所见,侦听器正在侦听文档,如果发现任何错误,则会抛出错误。

但是没有发现控制台中显示的这些Firebase错误:

Connection 1: encountered error(1:53)
6.26.0 - [Firebase/Firestore][I-FST000001] WatchStream (283fb4118) Stream error: 'Unavailable: Network connectivity changed'
6.26.0 - [Firebase/Firestore][I-FST000001] WatchStream (283fb4118) Stream error: 'Unavailable: DNS resolution failed'
6.26.0 - [Firebase/Firestore][I-FST000001] WatchStream (283fb4118) Stream error: 'Unavailable: DNS resolution failed'
6.26.0 - [Firebase/Firestore][I-FST000001] WatchStream (283fb4118) Stream error: 'Unavailable: failed to connect to all addresses'

我想念什么吗?

其他信息:我通过在手机上进入飞行模式来模拟这一点。否则就没有错误。

1 个答案:

答案 0 :(得分:0)

关于这个问题,您可能会觉得很有趣:how to catch error in firestore when no internet

这似乎是避免因为暂时没有连接而破坏应用程序的正常行为。

即使这个问题已经问了 7 个月,我也希望这对您有所帮助。