Flutter/cloud-firestore"任务已经完成"例外

Ric*_*oli 9 android dart firebase flutter google-cloud-firestore

我正在使用Flutter编写应用程序,我必须使用该Firestore.instance.runTransaction(Transaction tx)方法进行交易.在我的Transaction对象(或方法)中,我必须使用文档引用更新一些数据.

_firestore.runTransaction((Transaction x) async {
  await x.update(Aref, {'data': itemA - y});
  await x.update(Bref, {'data': itemB + y});
})
Run Code Online (Sandbox Code Playgroud)

代码运行时抛出异常(这里是控制台日志):

E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):无法处理方法调用结果E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):java.lang.IllegalStateException:任务已完成E/MethodChannel# plugins.flutter.io/cloud_firestore(32612):at com.google.android.gms.common.internal.Preconditions.checkState(Unknown来源:8)E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):at com .google.android.gms.tasks.zzu.zzdr(未知来源:8)E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):at com.google.android.gms.tasks.zzu.setResult(Unknown Source :3)E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):at com.google.android.gms.tasks.TaskCompletionSource.setResult(Unknown来源:2)E/MethodChannel#plugins.flutter.io/cloud_firestore( 32612):at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin $ 3 $ 1.success(CloudFirestorePlugin.java:283)E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):at io.flutter.plugin.common. MethodC hannel $ IncomingResultHandler.reply(MethodChannel.java:169)E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):at io.flutter.view.FlutterNativeView.handlePlatformMessageResponse(FlutterNativeView.java:187)E/MethodChannel#plugins. flutter.io/cloud_firestore(32612):在android.os.MessageQueue.nativePollOnce(Native Method)E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):at android.os.MessageQueue.next(MessageQueue.java:325 )E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):at android.os.Looper.loop(Looper.java:142)

小智 1

当同时调用 firestore api 时会发生错误,您必须将每个函数嵌套在前一个函数的“whenComplete(() {}”方法中。这是错误的代码:

g.f.runTransaction((transaction) async {
      DocumentSnapshot snap =
          await transaction.get(/my code);

      await transaction.update(/my code).whenComplete(() {});
    });


g.f.runTransaction((transaction) async {
      DocumentSnapshot freshSnap =
              await transaction.get(/my code));

      await transaction.update(/my code).whenComplete(() {});
});  //here is the problem!! I'have to nest this inside "whenComplete(() {})
Run Code Online (Sandbox Code Playgroud)

这是错误:

E/MethodChannel#plugins.flutter.io/cloud_firestore( 5337): 无法处理方法调用结果 E/MethodChannel#plugins.flutter.io/cloud_firestore( 5337): java.lang.IllegalStateException: 任务已完成

这是正确的代码

g.f.runTransaction((transaction) async {
  DocumentSnapshot snap =
      await transaction.get(/my code);

  await transaction.update(/my code).whenComplete(() {
    g.f.runTransaction((transaction) async {
      DocumentSnapshot freshSnap =
          await transaction.get(/my code);

      await transaction.update(/my code);
    }).whenComplete(() {});
  });
});
Run Code Online (Sandbox Code Playgroud)