【问题标题】:Update document value in Firestore Cloud with Flutter, having only a unique key value of such document使用 Flutter 更新 Firestore Cloud 中的文档值,只有此类文档的唯一键值
【发布时间】:2021-02-17 12:00:51
【问题描述】:

我是 Cloud Firestore 的新手,但已经进行了一些 CRUD 操作,但现在我真的被这件事困住了。

'tiposFrota' 集合中的每个文档都有一个键 'nome:' 具有唯一值,没有文档具有相同的 'nome:' 键值。

问题是,每当用户向其他集合添加另一个“卡车”时,我需要将“qtde:”的值加一,当他们删除“卡车”时,程序会将数字加 -1,作为柜台工作。

我设法创建了一个更新键值的操作,但仅当您拥有文档 ID 时,但在这种情况下,ID 是自动生成的,因为它们可能会从“tiposFrota”集合中添加或删除标准值。

FirebaseFirestore.instance
.collection('controladores')
.doc('contadores')
.update({'numFrota': FieldValue.increment(1)}),

我真的很困惑,如果有人可以帮忙的话。

谢谢!

【问题讨论】:

    标签: firebase flutter google-cloud-firestore cloud


    【解决方案1】:

    哇,自己找到了解决办法,这个帖子Get firestore collections based on values in array list in flutter

    由于 'nome:' 值对于 'tiposFrota' 集合中的每个文档都是唯一的,我可以使用 .where 语句作为所述文档的过滤器,获取所有文档的快照(但显然只获取一个)并在调用文档时使用'forEach'方法创建一个使用'.id'参数的函数。

    FirebaseFirestore.instance
    .collection('tiposFrota')
    .where('nome', isEqualTo: carMake)
    .get()
    .then((querySnapshot) { 
      querySnapshot.docs.forEach((element) {
         FirebaseFirestore.instance
         .collection('tiposFrota')
         .doc(element.id)
         .update({
           'qtde': FieldValue.increment(1)});
      });
    }),
    

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 2020-07-02
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 2018-11-01
      • 2020-02-04
      • 2018-05-08
      • 1970-01-01
      相关资源
      最近更新 更多