【发布时间】: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