Firestore交易引发异常

时间:2019-02-21 21:15:56

标签: firebase dart transactions flutter google-cloud-firestore

我正在尝试实施工资单应用程序。所以我想使用交易 问题是我想在不同的条件下停止交易:

找不到1个用户

2-数量不足

很想使用异常,但是问题是我得到了一个Java异常而不是我的错误,(任务是完全异常)

      await _firestoreClient.runTransaction((Transaction trans) async {
      DocumentReference userPocketRef =
          _firestoreClient.collection("users").document(userID);

      DocumentSnapshot userData = await trans.get(userPocketRef);

      if (!userData.exists) throw Exception("User Data Not Found");

      if (userData.data['cash'] == null || userData.data['cash'] < amount)
        throw Exception("Bill is Over Client Budget");

      await trans.update(userPocketRef, <String, dynamic>{
        "cash": userData.data['cash'] - amount,
      });
    })

我得到的例外是

E/MethodChannel#plugins.flutter.io/cloud_firestore(12228): Failed to handle E/MethodChannel#plugins.flutter.io/cloud_firestore(12228): Failed to handle method call result
java.lang.IllegalStateException: Task is already complete
at com.google.android.gms.common.internal.Preconditions.checkState(Unknown Source:29)
at com.google.android.gms.tasks.zzu.zzc(Unknown Source:123)
at com.google.android.gms.tasks.zzu.setException(Unknown Source:98)
at com.google.android.gms.tasks.TaskCompletionSource.setException(Unknown Source:11)
at io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin$3$1.error(CloudFirestorePlugin.java:296)
at io.flutter.plugin.common.MethodChannel$IncomingResultHandler.reply(MethodChannel.java:181)
at io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl.handlePlatformMessageResponse(FlutterNativeView.java:220)
at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessageResponse(FlutterJNI.java:211)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:326)
at android.os.Looper.loop(Looper.java:160)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

0 个答案:

没有答案