如何使用flatter在Firebase中的文档中保存documentID
我在Firebase中有一个名为favoritePost的集合,该集合有几个字段可以从我的应用程序中获取数据,其中一个字段是需要作为创建文档时生成的documentID的id,问题是,我使用了太多的代码来实现这个结果,并且在创建文档时,我还收到了大约2秒钟的错误 我想知道是否有更好的实现方法,以及如何修复错误 我在这里创建了这个集合 问题是,当创建集合时,我的主页中出现错误,因为此文档没有数据,它只是创建字段,我不能在字段中使用docref.documentId,因此我必须创建一个新方法来更新此集合并保存数据,包括docref.documentId 在这里,我将一些数据(包括docsref.documentID)传递到我的数据库文件 在这里,数据保存到以前创建的集合中,现在我可以将docsref.documentID包含到id字段中 您可以通过执行以下操作在主页中生成documentID:如何使用flatter在Firebase中的文档中保存documentID,firebase,flutter,dart,google-cloud-firestore,Firebase,Flutter,Dart,Google Cloud Firestore,我在Firebase中有一个名为favoritePost的集合,该集合有几个字段可以从我的应用程序中获取数据,其中一个字段是需要作为创建文档时生成的documentID的id,问题是,我使用了太多的代码来实现这个结果,并且在创建文档时,我还收到了大约2秒钟的错误 我想知道是否有更好的实现方法,以及如何修复错误 我在这里创建了这个集合 问题是,当创建集合时,我的主页中出现错误,因为此文档没有数据,它只是创建字段,我不能在字段中使用docref.documentId,因此我必须创建一个新方法来更新此
var randomDoc = await favRef
.document(widget.currentUserId)
.collection('favoritePost')
.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字段,而无需更新。您可以通过执行以下操作在主页中生成documentID:
var randomDoc = await favRef
.document(widget.currentUserId)
.collection('favoritePost')
.document();
String collectionRef= 'collectionRef';
String docId = Firestore.instance
.collection(collectionRef)
.document()
.documentID;
var docRef = Firestore.instance
.collection(collectionRef)
.document(docId);
通过使用无路径的文档,它将为您生成一个文档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字段,而无需更新
String collectionRef= 'collectionRef';
String docId = Firestore.instance
.collection(collectionRef)
.document()
.documentID;
var docRef = Firestore.instance
.collection(collectionRef)
.document(docId);
然后将数据设置为docRef,包括documentId:docId
然后将数据设置为docRef,包括documentId:docId您在主页中遇到的错误是什么?空值,但只需2秒钟。答案帮助您对其进行投票,并通过单击旁边的复选标记将其标记为正确,谢谢!您在主页中遇到的错误是什么?空值,但只需2秒钟。答案帮助您投票,并通过单击旁边的复选标记将其标记为正确,谢谢!