【问题标题】:Flutter: Updating firestore dataFlutter:更新 Firestore 数据
【发布时间】:2020-03-01 12:52:33
【问题描述】:

我试图更新文档中的值。我将文档称为

firestoreDB.collectionGroup('marks').firestore.document('chemsitry');

我得到了正确的文档 ID,但我得到了类似的错误

PlatformException(PlatformException(error, Invalid document reference. Document references 必须有偶数个段,但 stud001April2019 有 1, null)),

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    你现在写的归结为:

    firestoreDB.document('chemsitry');
    

    这意味着 Firestore 不知道文档在哪个集合中。


    如果您想更新marks 集合中的chemsitry [原文如此],您需要为该文档创建一个DocumentReference:

    firestoreDB.collection('marks').document('chemsitry');
    

    或略短:

    firestoreDB.document('marks/chemsitry');
    

    【讨论】:

    • 问题出在collectionGroup(...).firestore里面。如我的回答所示,您需要collection(...).document(...)。如果您自己找到了其他解决方案,请发帖作为答案。
    • 集合组仅在跨多个集合查询时有用。要更新文档,您需要有一个DocumentReference,这要求您知道要更新的集合和文档。我有一种感觉,我们在这里处理的是 XY problem,所以您能否更新您的问题以描述您尝试使用此代码完成的什么?
    • 我正在尝试将所有学生的分数放在一个列表中并最终确定每个分数,因此如果标记中有编辑,他们想从那里自行编辑。
    • 因此您已将标记加载到列表中,此时您必须知道标记所在的集合和文档 ID。而且您需要跟踪两者,以便以后可以更新该文档。我无法更好地解释它,所以如果这不能说清楚:更新您的问题以显示minimum, complete/standalone code that any of use can run to reproduce the problem。没有它,我们就没有足够的上下文。
    【解决方案2】:

    感谢您的关注,先生。我找到了解决方案

    我使用docRef = snapshot.reference.path 然后更新firestore.instance.document(docRef).updateData({ 'isVerified': true, 'verifiedOn': _time, });

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 1970-01-01
      • 2021-02-03
      • 2022-11-17
      • 2021-02-24
      • 2021-02-07
      • 2021-06-27
      • 2019-07-23
      • 1970-01-01
      相关资源
      最近更新 更多