gpt4 book ai didi

firebase - 您可以返回刚刚添加到集合中的文档的文档ID吗?

转载 作者:行者123 更新时间:2023-12-03 04:59:02 26 4
gpt4 key购买 nike

我想返回documentID,以便我可以存储它并在以后使用。我试图将createPrescription的返回值存储为动态类型,但是我也遇到了错误。

await DatabaseService().createPrescription(date, ic, illness, medication, important);

Future createPrescription(String newdate, String patientid, String sickness, String meds, String notes) async {
return await prescriptionCollection.document().setData({
'date' : newdate,
'patientID' : patientid,
'illness' : sickness,
'medication' : meds,
'important' : notes
});

}

最佳答案

您不应该使用await prescriptionCollection.document().setData(),因为您没有为document()提供任何ID。

如果您希望Firestore为您生成一个ID,则应改为await prescriptionCollection.add(),它返回对具有已生成ID的已创建文档的引用

您可以执行以下操作:

Future createPrescription(String newdate, String patientid, String sickness,
String meds, String notes) async {
var _ref = await prescriptionCollection.add({
'date': newdate,
'patientID': patientid,
'illness': sickness,
'medication': meds,
'important': notes
});
print(_ref.documentId);
}

关于firebase - 您可以返回刚刚添加到集合中的文档的文档ID吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59981611/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com