android - 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)
解决方案
当同时多次调用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(() {});
});
});
推荐阅读
- maximo - 无法将自定义对话框中的值设置为 Maximo 中的字段
- php - PHP将日期转换为时间
- excel - 使用标准访问 VBA 将 2 个表导出到一个 excel
- hangfire - Hangfire 对 SlidingInvisibilityTimeout 的说明
- c - 从正则表达式到 NFA 再到 DFA
- java - Java:将 ArrayList 与 JComboBox 和 JTextField 一起使用
- node.js - 我可以使用 ES6 模块(导入导出)来编写 webpack 配置文件吗?
- java - Spring 不能再在 Java-13 中通过名称识别唯一的 bean
- ios - 沙盒中的自动续订订阅:重新启动应用后不会续订
- r - R中的变异列:根据另一列条件从列中减去值