使用 runTransaction 的 Flutter/Firebase Firestore 什么也不做

use*_*552 3 dart firebase flutter google-cloud-firestore

嗨,我正在尝试在 Flutter 中使用 Firebase Firestore 实现简单的事务。该过程很简单,只需更新两个文件,如下所示:

DocumentSnapshot productSnapshot = await productReference.get();
    DocumentSnapshot userSnapshot = await userReference.get();
    DocumentSnapshot userProductSnapshot = await userProductReference.get();
    if (productSnapshot.exists &&
        userSnapshot.exists &&
        userProductSnapshot.exists) {
      double price = double.parse(productSnapshot.data["productPrice"].toString());
      double money = double.parse(userSnapshot.data["money"].toString());
      double afterBuyMoney = money - price;
      if (afterBuyMoney >= 0) {
        await userReference.updateData({"money": afterBuyMoney});
        await userProductReference.updateData(({"isOwned": true}));
        response = "success";
      }
    }
Run Code Online (Sandbox Code Playgroud)

此代码已经运行良好并获得预期结果,并且数据在云数据库中更新,但是因为有两个事务并且中间事务可能存在错误。我想使用 Firestore runTransaction 来实现这个,所以如果出现错误,所有的事务都将被回滚。

这是我使用 Firestore runTransaction 的代码

await Firestore.instance.runTransaction((transaction) async {
      DocumentSnapshot productSnapshot = await transaction.get(productReference);
      DocumentSnapshot userSnapshot = await transaction.get(userReference);
      DocumentSnapshot userProductSnapshot = await transaction.get(userProductReference);
      if(productSnapshot.exists && userSnapshot.exists && userProductSnapshot.exists) {
        double price = double.parse(productSnapshot.data["productPrice"].toString());
        double money = double.parse(userSnapshot.data["money"].toString());
        double afterBuyMoney = money - price;
        if (afterBuyMoney >= 0) {
          await transaction.update(userReference, {"money": afterBuyMoney});
          await transaction.update(userProductReference,({"isOwned": true}));
          response = "success";
        }
      }
    }).then((_) {
      print("Success");
    }).catchError((error) {
      response = error.message;
    });
Run Code Online (Sandbox Code Playgroud)

此代码也可以正常工作并打印“成功”,但是在 firestore 数据库中,数据未更新,尽管日志中没有错误,但交易似乎不起作用。我的代码中是否有任何遗漏的步骤或错误?如何解决这个问题?

小智 6

尝试:

Future<bool> insert() async {
    String document = "document";
    String collection = "collection";
    Map map = new Map();
    map["last_name"] = "Last Name";
    map["first_name"] = "First Name";
    map["middle_name"] = "Middle Name";

    DocumentReference documentReference = Firestore.instance.collection(collection).document(document);
    Firestore.instance.runTransaction((transaction) async {
      await transaction
          .set(documentReference, {
            'last_name': map['last_name'],
            'first_name': map['first_name'],
            'middle_name': map['middle_name']
          })
          .catchError((e) {})
          .whenComplete(() {});
    }).catchError((e) {
      return false;
    });

    return true;
}
Run Code Online (Sandbox Code Playgroud)


Ala*_*ley 0

在这里提供了一个可能的解决方案,即切换您正在使用的 Flutter 版本。