【发布时间】:2020-01-23 10:52:12
【问题描述】:
在以下 Firestore 事务中,如果条件 (someNumber
例外情况:
PlatformException(PlatformException(9, Transaction failed all retries.: 在事务中读取的每个文档也必须写入该事务中。, null))
交易:
_myFunction({@required docId,}) {
try {
final DocumentReference docRef = Firestore.instance.collection('collection').document(docId);
Firestore.instance.runTransaction(
(tx) async {
DocumentSnapshot snapshot = await tx.get(docRef);
var someNumber = snapshot['someNumber'] ?? 0;
if (snapshot.exists && someNumber < 2) {
await tx.update(
docRef,
{
'someNumber': FieldValue.increment(1),
},
);
} else {
print("someNumber exceeded");
return;
}
},
);
} catch (e) {
print(e);
}
}
如果 someNumber 小于 2,则事务正常工作。需要在事务内部读取此变量,因为两个用户可能同时更新该值。
【问题讨论】:
标签: flutter google-cloud-firestore