如何使用runTransaction升级两个不同集合中的文档?

时间:2020-03-11 01:25:27

标签: firebase flutter google-cloud-firestore

当我尝试使用Firestore.instance.runTransaction更新用户的余额时,我遇到以下问题,该问题会执行,但是当我尝试为将要从中收取款项的驾驶员进行相同的交易时用户,并且不会执行。

我试图在同一个“ Firestore.instance.runTransaction”中输入这两个事务,但是由于产生的snapdata是来自用户而不是来自驱动程序,因此它显然不能满足我的需要。

  Firestore.instance.runTransaction((Transaction tran) async {
              await tran
                  .get(Firestore.instance
                      .collection('users')
                      .document(currentUser.uid))
                  .then((DocumentSnapshot snap) {
                if (snap.exists) {
                  //Upgrade Balance of the User
                  tran.update(
                      Firestore.instance
                          .collection('users')
                          .document(currentUser.uid),
                      <String, dynamic>{
                        "Balance": snap.data['Balance'] - actcosto
                      }).catchError((err) => print(err)); 

                }
              });
            });

            Firestore.instance.runTransaction((Transaction tran) async {
              await tran
                  .get(Firestore.instance
                      .collection('drivers')
                      .document(driverInputController.text))
                  .then((DocumentSnapshot snap) {
                if (snap.exists) {
                  //Upgrade Balance of the driver
                  tran.update(
                      Firestore.instance
                          .collection('drivers')
                          .document(driverInputController.text),
                      <String, dynamic>{
                        "Balance": snap.data['Balance'] + actcosto
                      }).catchError((err) => print(err)); 

                }
              });
            });

出现此错误

E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
I/chatty  (18963): uid=10282(ml.remolquerosdelsurpzo.firebasetesis) identical 4 lines
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
I/chatty  (18963): uid=10282(ml.remolquerosdelsurpzo.firebasetesis) identical 3 lines
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
I/chatty  (18963): uid=10282(ml.remolquerosdelsurpzo.firebasetesis) identical 1 line
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/AccessibilityBridge(18963): VirtualView node must not be the root node.
E/CloudFirestorePlugin(18963): java.lang.Exception: DoTransaction failed: Invalid document reference. Document references must have an even number of segments, but drivers has 1
E/CloudFirestorePlugin(18963): java.util.concurrent.ExecutionException: java.lang.Exception: DoTransaction failed: Invalid document reference. Document references must have an even number of segments, but drivers has 1
E/CloudFirestorePlugin(18963):  at com.google.android.gms.tasks.Tasks.zzb(Unknown Source:61)
E/CloudFirestorePlugin(18963):  at com.google.android.gms.tasks.Tasks.await(Unknown Source:33)
E/CloudFirestorePlugin(18963):  at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$4.apply(CloudFirestorePlugin.java:527)
E/CloudFirestorePlugin(18963):  at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$4.apply(CloudFirestorePlugin.java:479)
E/CloudFirestorePlugin(18963):  at com.google.firebase.firestore.FirebaseFirestore.lambda$runTransaction$0(com.google.firebase:firebase-firestore@@21.3.0:301)
E/CloudFirestorePlugin(18963):  at com.google.firebase.firestore.FirebaseFirestore$$Lambda$5.call(Unknown Source:6)
E/CloudFirestorePlugin(18963):  at com.google.android.gms.tasks.zzv.run(Unknown Source:2)
E/CloudFirestorePlugin(18963):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
E/CloudFirestorePlugin(18963):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
E/CloudFirestorePlugin(18963):  at java.lang.Thread.run(Thread.java:764)
E/CloudFirestorePlugin(18963): Caused by: java.lang.Exception: DoTransaction failed: Invalid document reference. Document references must have an even number of segments, but drivers has 1
E/CloudFirestorePlugin(18963):  at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$4$1$1.error(CloudFirestorePlugin.java:509)
E/CloudFirestorePlugin(18963):  at io.flutter.plugin.common.MethodChannel$IncomingResultHandler.reply(MethodChannel.java:210)
E/CloudFirestorePlugin(18963):  at io.flutter.embedding.engine.dart.DartMessenger.handlePlatformMessageResponse(DartMessenger.java:111)
E/CloudFirestorePlugin(18963):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessageResponse(FlutterJNI.java:652)
E/CloudFirestorePlugin(18963):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/CloudFirestorePlugin(18963):  at android.os.MessageQueue.next(MessageQueue.java:325)
E/CloudFirestorePlugin(18963):  at android.os.Looper.loop(Looper.java:142)
E/CloudFirestorePlugin(18963):  at android.app.ActivityThread.main(ActivityThread.java:6626)
E/CloudFirestorePlugin(18963):  at java.lang.reflect.Method.invoke(Native Method)
E/CloudFirestorePlugin(18963):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
E/CloudFirestorePlugin(18963):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
E/flutter (18963): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(Error performing transaction, Every document read in a transaction must also be written., null)
[38;5;244mE/flutter (18963): #0      StandardMethodCodec.decodeEnvelope[39;49m
[38;5;244mE/flutter (18963): #1      MethodChannel.invokeMethod[39;49m
E/flutter (18963): <asynchronous suspension>
[38;5;244mE/flutter (18963): #2      MethodChannel.invokeMapMethod[39;49m
[38;5;248mE/flutter (18963): #3      Firestore.runTransaction[39;49m
[38;5;248mE/flutter (18963): #4      HomePageState._showDialog.<anonymous closure>[39;49m
[38;5;244mE/flutter (18963): #5      _InkResponseState._handleTap[39;49m
[38;5;244mE/flutter (18963): #6      _InkResponseState.build.<anonymous closure>[39;49m
[38;5;244mE/flutter (18963): #7      GestureRecognizer.invokeCallback[39;49m
[38;5;244mE/flutter (18963): #8      TapGestureRecognizer.handleTapUp[39;49m
[38;5;244mE/flutter (18963): #9      BaseTapGestureRecognizer._checkUp[39;49m
[38;5;244mE/flutter (18963): #10     BaseTapGestureRecognizer.handlePrimaryPointer[39;49m
[38;5;244mE/flutter (18963): #11     PrimaryPointerGestureRecognizer.handleEvent[39;49m
[38;5;244mE/flutter (18963): #12     PointerRouter._dispatch[39;49m
[38;5;244mE/flutter (18963): #13     PointerRouter._dispatchEventToRoutes.<anonymous closure>[39;49m
[38;5;244mE/flutter (18963): #14     _LinkedHashMapMixin.forEach  (dart:collection-patch/compact_hash.dart:379:8)[39;49m
[38;5;244mE/flutter (18963): #15     PointerRouter._dispatchEventToRoutes[39;49m
[38;5;244mE/flutter (18963): #16     PointerRouter.route[39;49m
[38;5;244mE/flutter (18963): #17     GestureBinding.handleEvent[39;49m
[38;5;244mE/flutter (18963): #18     GestureBinding.dispatchEvent[39;49m
[38;5;244mE/flutter (18963): #19     GestureBinding._handlePointerEvent[39;49m
[38;5;244mE/flutter (18963): #20     GestureBinding._flushPointerEventQueue[39;49m
[38;5;244mE/flutter (18963): #21     GestureBinding._handlePointerDataPacket[39;49m
[38;5;244mE/flutter (18963): #22     _rootRunUnary  (dart:async/zone.dart:1138:13)[39;49m
[38;5;244mE/flutter (18963): #23     _CustomZone.runUnary  (dart:async/zone.dart:1031:19)[39;49m
[38;5;244mE/flutter (18963): #24     _CustomZone.runUnaryGuarded  (dart:async/zone.dart:933:7)[39;49m
[38;5;244mE/flutter (18963): #25     _invoke1  (dart:ui/hooks.dart:273:10)[39;49m
[38;5;244mE/flutter (18963): #26     _dispatchPointerDataPacket  (dart:ui/hooks.dart:182:5)[39;49m
E/flutter (18963):

0 个答案:

没有答案