Mic*_*ael 5 firebase flutter google-cloud-firestore
我正在尝试使用 Flutter/Dart 和 Firestore创建一个保存事务,它执行以下操作:
单个文档如下所示:
{
"title": "Some title",
"position": 1
}
Run Code Online (Sandbox Code Playgroud)
在文档(https://firebase.google.com/docs/firestore/manage-data/transactions)中,我找到了有关如何在单个文档上进行保存交易的说明,但没有找到有关如何创建此“两步- atomic-transaction”,我需要在集合中保存插入和更新。
也许有人对我有暗示?
更新: 感谢您的输入弗兰克。这是一个代码片段,希望能解释我想要实现的目标:
@override
Future addCatalogItem(CatalogItem catalogItem) {
return firestore.runTransaction((Transaction transaction) async {
// Update position (++) of existing catalogItems
await firestore
.collection(path)
.where("catalogId", isEqualTo: catalogItem.catalogId)
.snapshots()
.forEach((snapshot) {
snapshot.documents.forEach((document) {
// document.reference.updateData({"position": document.data["position"]});
transaction.update(document.reference, {"position": document.data["position"] + 1});
});
});
// Add the new catalogItem at position 1
await firestore
.collection(path)
.document(catalogItem.id)
.setData(catalogItem.toJson());
});
Run Code Online (Sandbox Code Playgroud)
}
归档时间: |
|
查看次数: |
3827 次 |
最近记录: |