使用 flutter 增加 firestore 中的值

Meh*_*mir 1 firebase flutter google-cloud-firestore

您好,如果 firestore 中有数据,我尝试在单击按钮时增加一个值,这是我的代码块,如果您有任何建议,请 lmk

 int q = 0;

    final snapShot = await Firestore.instance.collection('cart').document("LIihBLtbfuJ8Dy640DPd").get();
    if(snapShot.exists){
      q=q+1;


    }
    Firestore.instance.runTransaction((Transaction transaction) async {
      await transaction.update(
          Firestore.instance
              .collection("cart")
              .document("LIihBLtbfuJ8Dy640DPd"),
          {
            foodItem.name: {
              'itemName': foodItem.name,
              'imgUrl': foodItem.imageAssetPath,
              'itemPrice': foodItem.price,
              'quantity': q,
            }
          });

    });
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

Mad*_*mar 5

2021 年 11 月,这对我有用。

 FirebaseFirestore.instance.collection('users').doc(currentUser?.id).update({
      'bronzeBadges': FieldValue.increment(2),
    });
Run Code Online (Sandbox Code Playgroud)