首页 > 解决方案 > Flutter/cloud-firestore“任务已经完成”异常

问题描述

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

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

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

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 Source: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):在 com.google.android.gms.tasks.zzu.setResult(未知来源:3) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612): at com.google.android.gms.tasks.TaskCompletionSource.setResult(Unknown Source:2) E/MethodChannel#plugins.flutter.io/cloud_firestore( 32612):在 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.MethodChannel$IncomingResultHandler.reply(MethodChannel.java:169) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):在 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): 在 android.os.Looper.loop(Looper.java:142)io/cloud_firestore(32612):在 io.flutter.plugin.common.MethodChannel$IncomingResultHandler.reply(MethodChannel.java:169) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):在 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 ): 在 android.os.MessageQueue.next(MessageQueue.java:325) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612): 在 android.os.Looper.loop(Looper.java:142)io/cloud_firestore(32612):在 io.flutter.plugin.common.MethodChannel$IncomingResultHandler.reply(MethodChannel.java:169) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612):在 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 ): 在 android.os.MessageQueue.next(MessageQueue.java:325) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612): 在 android.os.Looper.loop(Looper.java:142)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):在 android.os.MessageQueue.next(MessageQueue.java:325) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612): 在 android.os.Looper.loop(Looper.java:142)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):在 android.os.MessageQueue.next(MessageQueue.java:325) E/MethodChannel#plugins.flutter.io/cloud_firestore(32612): 在 android.os.Looper.loop(Looper.java:142)

标签: androidfirebasedartgoogle-cloud-firestoreflutter

解决方案


当同时多次调用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(() {})

这是错误:

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(() {});
  });
});

推荐阅读