离线尝试后,Firestore访问中断

时间:2018-06-27 10:14:23

标签: firebase dart google-cloud-firestore flutter

我正在尝试使用这段代码访问Cloud Firestore:

void _submit(BuildContext context) async {
    final DocumentReference postRef = Firestore.instance.document(dbPath);
    Firestore.instance.runTransaction((transaction) async {
        DocumentSnapshot freshSnap = await transaction.get(postRef);
        await transaction.update(freshSnap.reference, {
            'value': freshSnap['value'] + 1
        });
   });
}

如果打开wifi或移动数据,一切正常。 (符合预期

如果wifi和移动数据已关闭,则无法使用。 (符合预期)。但是当我等到超时(调用该方法之后),然后才打开移动数据和wifi时,它不再起作用,并且出现以下错误:

E/flutter ( 7041): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 7041): PlatformException(Error performing transaction, Timed out waiting for Task, null)
E/flutter ( 7041): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:547:7)
E/flutter ( 7041): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:279:18)
E/flutter ( 7041): <asynchronous suspension>
E/flutter ( 7041): #2      Firestore.runTransaction (file:///C:/{myPath}/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.7.3/lib/src/firestore.dart:115:10)
E/flutter ( 7041): <asynchronous suspension>
E/flutter ( 7041): #3      _FeedbackPageState._submitFeedback (package:appli_salon_data/view/program/FeedbackPage.dart:74:26)
E/flutter ( 7041): <asynchronous suspension>
E/flutter ( 7041): #4      _FeedbackPageState.build.<anonymous closure> (package:appli_salon_data/view/program/FeedbackPage.dart:60:26)
E/flutter ( 7041): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:494:14)
E/flutter ( 7041): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:549:30)
E/flutter ( 7041): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter ( 7041): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
E/flutter ( 7041): #9      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123:7)
E/flutter ( 7041): #10     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 7041): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
E/flutter ( 7041): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
E/flutter ( 7041): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
E/flutter ( 7041): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
E/flutter ( 7041): #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
E/flutter ( 7041): #16     _invoke1 (dart:ui/hooks.dart:134:13)
E/flutter ( 7041): #17     _dispatchPointerDataPacket (dart:ui/hooks.dart:91:5)

不期望

在重新启动应用程序之前,该方法将不再起作用。

如果有人对此行为有一些解释,请随时回答:)

更新:我尝试过:

  1. 关闭Wifi
  2. 尝试运行事务-导致预期的失败
  3. 打开Wifi
  4. 尝试运行事务

这是有趣的部分:在第4步中,交易“立即”运行了两次,两次都出现错误:

PlatformException(Error performing Transaction#get, UNAVAILABLE: Unable to resolve host firestore.googleapis.com, null)

致电transaction.get(postRef)时。

这是否意味着Firestore在短暂失去Internet连接一次后就以某种方式失去了对主机的所有访问权限?我该如何解决?

1 个答案:

答案 0 :(得分:1)

您是否尝试过启用本地持久性?不知道这是否能解决问题,但值得一试。我相信可以使用persistenceEnabled方法中的settings参数来完成此操作:

Firestore.instance.settings(persistenceEnabled: true)

请记住,此操作只能执行一次,因此它应该放在代码中的某个位置,每次访问任何Firestore数据都不会被调用。