我是 Flutter 新手,我想知道是否有一种方法可以使用其先前的状态更新 Firestore 中的一个字段。我的文档中有一个 counter: 0
,我试图将其增加 1 或减少 2。
我怎样才能实现这个目标?
最佳答案
Firestore 有一个特定的运算符,称为 FieldValue.increment()
文档:https://firebase.google.com/docs/firestore/manage-data/add-data#increment_a_numeric_value
var washingtonRef = db.collection('cities').doc('DC');
// Atomically increment the population of the city by 50.
washingtonRef.update({
population: firebase.firestore.FieldValue.increment(50)
});
如果要递减,请使用负值:
FieldValue.increment(-50)
关于firebase - Flutter:如何使用先前的状态更新 Firestore 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65110505/