[英]Flutter Firestore : Add a List of Data to Firestore Document
[英]How to add List of Object to Firestore?
我有一個名為customStep
的模型,我想將名為customSteps
的 customStep 列表推送到 firestore。
這是代碼:
Firestore.instance
.collection('customSteps')
.add({'customSteps': customSteps});
customSteps 集合有文檔,這些文檔由 customSteps 字段組成,用於存儲 customStep 數組。 但是,此代碼將空數組推送到 firestore。 我該如何解決?
要將對象推送到 Firestore,您需要將對象轉換為映射,將此函數添加到您的類中:
Map<String, dynamic> toMap() {
return {
'yourField1': yourValue1,
'yourField2': yourValue1,
};
}
要推送自定義步驟列表,您需要將所有對象轉換為地圖,您可以使用以下方法完成:
static List<Map> ConvertCustomStepsToMap({List<CustomStep> customSteps}) {
List<Map> steps = [];
customSteps.forEach((CustomStep customStep) {
Map step = customStep.toMap();
steps.add(step);
});
return steps;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.