![](/img/trans.png)
[英]how to get data from cloud firestore where user.uid equal to document id in flutter?
[英]How to get the document id from a firestore document in Flutter?
在我的應用程序中,用戶可以有許多不同的健身計划。 所以我有一個集合,其中每個 gymplan 都有自己的文件。 創建新計划時,我想將文檔 ID 存儲在文檔中,以便我可以使用該 ID 訪問該文檔。
創建新文檔時,Firestore 會自動創建一個唯一的 ID,這很好。 但是我怎樣才能在我的代碼中得到這個 id 呢? 到目前為止,我創建新計划的代碼如下所示:
Future createPlan(String planName, List exerciseNames, List rows) async {
return await usersCol.doc(myUser.uid).collection('plans').add({
'planId': /// here i want to save the document id firestore creates
'name': planName,
'exerciseNames': exerciseNames,
'rows': rows,
});
}
您必須先創建文檔。 然后使用set()
而不是add()
。 所以:
final ref = usersCol.doc(myUser.uid).collection('plans').doc();
return await ref.set({
'planId': ref.id,
'name': planName,
'exerciseNames': exerciseNames,
'rows': rows,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.