首页 > 解决方案 > 如果更新另一个数据,firestore颤振会重新计算存储的所有数据

问题描述

在此处输入图像描述我将数据存储在 firebase 中,如下所示

计划开始和计划完成以数学方式计算计划完成 = 计划开始 + 计划持续时间 计划开始 = 依赖 sl no 的计划完成 sl no 10 依赖是 9 所以计划 10 开始等于计划完成 9 所以我的问题是如果我更新了 sl no 9 的计划完成,那么我怎样才能让 10 自动更新它计划的开始和完成关于如何保存数据的代码如下

 if (slno != null && activity != null) {
                      if (action == 'create') {
                        // Persist a new product to Firestore
                        Future<Timestamp> getslno() async {
                          var document =
                              await _schedule.doc(dependent.toString()).get();
                          Timestamp sl = await document['plannedfinish'];
                          return sl;
                        }

                        var planneds = await getslno();
                        await _schedule.doc(slno.toString()).set({
                          "slno": slno,
                          "activity": activity,
                          "duration": duration,
                          "dependent": dependent,
                          "plannedstart": planneds,
                          "plannedfinish": planneds
                              .toDate()
                              .add(Duration(days: duration!.toInt()))
                        });
                      }

                      if (action == 'update') {
                        // Update the product
                        await _schedule.doc(documentSnapshot!.id).update({
                          "slno": slno,
                          "activity": activity,
                          "duration": duration,
                          "dependent": dependent
                        });
                      }

如果至少有一个数据发生更改,我需要自动更新所有存储的数据 有关如何执行此操作的任何建议

标签: firebaseflutter

解决方案


使用firebase云功能,并使用文档onUpdate。即使你不懂 JavaScript,函数逻辑也很容易在你的方程中实现,并且用于 firestore 的 firebase admin SDK 在很大程度上类似于你在 Flutter 中编写的内容。这将是理想的解决方案,否则,每当有人更新文档时,您都需要在应用程序中使用另一个函数来获取文档并重新计算,然后在客户端再次更新它,这不是很受欢迎。要启用云功能,它会要求提供计费信息,但在 200 万次 API 调用之后才会向您计费。强烈建议您开车进入并释放力量。


推荐阅读