我正在读取Firestore事务中的几条记录并更新一些记录。如果不是,则所有记录更新的事务失败。它尝试重新运行事务,但由于平台异常“事务已完成”而失败。随附日志和简化的代码示例。
日志:
I/flutter (11935): Transaction try
I/flutter (11935): Transaction try
I/flutter (11935): >>>>>>>>>>>>>> Exception within transaction <<<<<<<<<<<<<<<<
I/flutter (11935): PlatformException(Error performing Transaction#get, Transaction has already completed., null)
I/flutter (11935): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
I/flutter (11935): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
I/flutter (11935): <asynchronous suspension>
I/flutter (11935): #2 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:344:48)
I/flutter (11935): <asynchronous suspension>
I/flutter (11935): #3 Transaction._get (package:cloud_firestore/src/transaction.dart:27:10)
I/flutter (11935): <asynchronous suspension>
I/flutter (11935): #4 Transaction.get (package:cloud_firestore/src/transaction.dart:20:45)
I/flutter (11935): #5 _MyAppState.build.<anonymous closure>.<anonymous closure> (package:biblosphere/main.dart:391:54)
I/flutter (11935): <asynchronous suspension>
I/flutter (11935): #6 new Firestore.<anonymous closure> (package:cloud_firestore/src/firestore.dart:30:54)
I/flutter (11935): <asynchronous suspension>
I/flutter (11935): #7 MethodChannel._handleAsMethodCall (package:flutter/src/services/platform_channel.dart:397:55)
I/flutter (11935): <asynchronous suspension>
I/flutter (11935): #8 MethodChannel.setMethodCallHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:365:
代码示例:
DocumentReference docRef1 = Firestore.instance.collection('col1').document();
await docRef1.setData({'name': 'John'});
DocumentReference docRef2 = Firestore.instance.collection('col1').document();
await docRef2.setData({'number': 0}); // This code fails
//await docRef2.setData({'number': 1}); // This code works
await db.runTransaction((Transaction tx) async {
try {
print('Transaction try');
DocumentSnapshot docSnap1 = await tx.get(docRef1);
if (!docSnap1.exists)
throw ('Record does not exist in DB');
DocumentSnapshot docSnap2 = await tx.get(docRef2);
if (!docSnap2.exists)
throw ('Record does not exist in DB');
if (docSnap2.data['number'] == 0) {
// Pay reward to the owner
await tx.update(docRef1, {'name': 'Peter'});
} else {
await tx.update(docRef1, {'name': 'Mary'});
await tx.update(docRef2, {'number': 2});
}
} catch (ex, stack) {
print(
'>>>>>>>>>>>>>> Exception within transaction <<<<<<<<<<<<<<<<');
print(ex);
print(stack);
}
});
我扑扑的医生:
[√] Flutter (Channel stable, v1.9.1+hotfix.5, on Microsoft Windows [Version 10.0.18362.418], locale en-US)
• Flutter version 1.9.1+hotfix.5 at D:\Flutter
• Framework revision 1aedbb1835 (4 days ago), 2019-10-17 08:37:27 -0700
• Engine revision b863200c37
• Dart version 2.5.0
[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at C:\Users\DenisStark\AppData\Local\Android\sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 28.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
• All Android licenses accepted.
[√] Android Studio (version 3.3)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 33.3.1
• Dart plugin version 182.5215
• Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
[!] VS Code (version 1.39.2)
• VS Code at C:\Users\DenisStark\AppData\Local\Programs\Microsoft VS Code
X Flutter extension not installed; install from
https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter
[√] Connected device (1 available)
• VKY L29 • QYJ7N17524000190 • android-arm64 • Android 9 (API 28)
! Doctor found issues in 1 category.
我可以通过对事务中读取的所有记录进行虚拟更新来解决。但是我怀疑这是cloud_firestore插件中的错误。您有什么建议?