首页 > 解决方案 > 扑扑 Firestore 交易这行得通吗?

问题描述

我正在尝试更新值,Transaction首先我尝试过这样,但应用程序会在tx.get. 我认为这是插件问题我在 git 中看到了几个问题。

 await Firestore.instance.runTransaction((tx) async {
    DocumentReference ref = Firestore.instance.collection('votes').document('6z2C2cSwnHNXaRNmzs4P');
    DocumentSnapshot snap = await tx.get(ref); // crash here
    if (snap.exists) {
      await tx.update(snap.reference, {'vote': snap.data['vote'] + 1});
    } 
  });

我刚刚更改tx.getref.get,它在更新值方面起作用。
那么我想问的是,如果我这样改变,这仍然可以原子地工作吗?

 await Firestore.instance.runTransaction((tx) async {
    DocumentReference ref = Firestore.instance.collection('votes').document('6z2C2cSwnHNXaRNmzs4P');
    DocumentSnapshot snap = await ref.get(); // changed here
    if (snap.exists) {
      await tx.update(snap.reference, {'vote': snap.data['vote'] + 1});
    }
  });

标签: fluttergoogle-cloud-firestore

解决方案


推荐阅读