【问题标题】:CRUD flutter/firestore/firebaseCRUD颤动/firestore/firebase
【发布时间】:2020-03-25 15:35:27
【问题描述】:

我是编码新手,我想要实现的是设置一种方法来检查帖子中的“星期一”集合以及数据是否存在?字段的值会更新,否则它们会被创建!我已经被困了一段时间,并被重定向到不同的解决方案,但没有任何东西对我有用。感谢所有帮助

  Future<void> createPostMonday(Post post) async{
    await postsRef.document(post.authorId).collection('Monday').setData({
      'alOne':post.alOne,
      'alTwo':post.alTwo,
      'alThree':post.alThree,
      'alFour':post.alFour,
      'alFive':post.alFive,
      'alSix':post.alSix,
      'beOne':post.beOne,
      'beTwo':post.beTwo,
      'beThree':post.beThree,
      'beFour':post.beFour,
      'beFive':post.beFive,
      'beSix':post.beSix,
      'likes': post.likes,
      'authorId': post.authorId,
      'timestamp': post.timestamp,
    });

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

    setData() 用于写入您已经知道其 ID 的单个文档的内容。它要求您使用DocumentReference 对象来定位该文档。

    如果您尝试将新文档添加到具有唯一随机 ID 的集合中,则应使用 CollectionReferenceadd() 方法。

    await postsRef.document(post.authorId).collection('Monday').add(...)
    

    【讨论】:

    • 嗨@Doug Stevenson。先感谢您。我想做的是创建一个检查集合是否已经存在的方法?更新它们,否则创建新的。我已经搜索并得到了不同的解决方案的建议,所以我来到了这里!
    • 您刚才所说的听起来与您在帖子中所说的完全不同。请编辑问题以非常清楚您要达到的目标。现在,您的所有问题只是说您在尝试使用 API 时遇到了编译错误。
    猜你喜欢
    • 2021-10-25
    • 2021-04-18
    • 2021-04-18
    • 2020-06-29
    • 2019-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    相关资源
    最近更新 更多