【问题标题】:How to save the documentID in a document in Firebase using Flutter如何使用 Flutter 将 documentID 保存在 Firebase 中的文档中
【发布时间】:2020-08-07 14:10:28
【问题描述】:

我在 Firebase 中有一个名为 favoritePost 的集合,该集合有几个从我的应用程序获取数据的字段,其中一个字段是 id,它必须是在文档生成时生成的 documentID创建后,问题是我使用了太多代码来实现此结果,并且在创建文档时我也收到了大约 2 秒的错误。

我想知道是否有更好的实现方法以及如何修复错误。

我在这里创建集合

问题就在这里,创建集合时,我的主页出现错误,因为该文档没有数据,它只是创建字段,我不能在字段内使用docref.documentId,所以我有创建一个新方法来更新此集合并保存包括docref.documentId 在内的数据。

final docsref = await favRef
          .document(widget.currentUserId)
          .collection('favoritePost')
          .add(
        {
          'imageUrl': '',
          'caption': '',
          'likeCount': 0,
          'authorId': '',
          'timestamp': '',
          'userName': '',
          'userImage': '',
          'id': '',
          'idSecondPost': '',
          'experience': '',
          'cSrateStars': '',
          'productName': '',
          'brand': '',
          'cost': '',
          'envioDiasRateStars': '',
          'easyPurchaseRateStars': '',
          'totalRate': '',
          'businessName': '',
          'recomendation': '',
          'videoLink': '',
        },
      );

这里我将包括docsref.documentID 在内的一些数据传递到我的数据库文件中

DatabaseService.addFavorite(
          currentUserId: widget.currentUserId,
          post: widget.reviews,
          docref: docsref.documentID);

这里的数据保存到之前创建的集合中,现在我可以将docsref.documentID 包含到id 字段中

static void addFavorite(
      {String idReview,
      Reviews post,
      String currentUserId,
      String toId,
      String docref}) {
    DocumentReference postRef = reviewRef.document(post.idReview);
    postRef.get().then(
      (doc) {
        final docsref = favRef
            .document(currentUserId)
            .collection('favoritePost')
            .document(docref)
            .updateData({
          'imageUrl': post.imageUrl,
          'caption': post.comments,
          'likeCount': post.likeCount,
          'authorId': post.authorId,
          'timestamp': post.timestamp,
          'userName': post.userName,
          'userImage': post.userImage,
          'idReview': post.idReview,
          'experience': post.address,
          'cSrateStars': post.cSrateStars,
          'productName': post.productName,
          'brand': post.brand,
          'cost': post.cost,
          'envioDiasRateStars': post.envioDiasRateStars,
          'easyPurchaseRateStars': post.easyPurchaseRateStars,
          'totalRate': post.totalRate,
          'businessName': post.businessName,
          'recomendation': post.recomendation,
          'id': docref,
          'videoLink': post.videoLink,
        });
      },
    );
  }

【问题讨论】:

  • 您在主页中遇到的错误是什么?
  • 空值,但只需要 2 秒
  • 由于答案对您有所帮助,请点赞并点击旁边的复选标记将其标记为正确,谢谢!

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

您可以通过执行以下操作在主页中生成documentID

var randomDoc = await favRef
          .document(widget.currentUserId)
          .collection('favoritePost')
          .document();

使用document()不带路径,它会为你生成一个文档id,然后你可以这样做:

final docsref = await favRef
          .document(widget.currentUserId)
          .collection('favoritePost')
          .document(randomDoc.documentID)
          .setData(
        {
          'imageUrl': '',
          'caption': '',
          'likeCount': 0,
          'authorId': '',
          'timestamp': '',
          'userName': '',
          'userImage': '',
          'id': '',
          'idSecondPost': '',
          'experience': '',
          'cSrateStars': '',
          'productName': '',
          'brand': '',
          'cost': '',
          'envioDiasRateStars': '',
          'easyPurchaseRateStars': '',
          'totalRate': '',
          'businessName': '',
          'recomendation': '',
          'videoLink': '',
          'docId' : randomDoc.documentID
        },
      );

这样你就可以立即在首页生成id并将其添加到docId字段中,无需更新。

【讨论】:

    【解决方案2】:
    String collectionRef= 'collectionRef';
    String docId = Firestore.instance
        .collection(collectionRef)
        .document()
        .documentID;
    var docRef = Firestore.instance
        .collection(collectionRef)
        .document(docId);
    

    然后setDatadocRef 包括documentId: docId

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 2020-04-09
      • 2019-10-03
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      相关资源
      最近更新 更多