我正在尝试使用 firestore 为我的应用程序添加持久性。看起来不错,但我在路上遇到了一个颠簸,谷歌搜索似乎让我失望。我认为这可能更像是 dart 问题而不是 firestore,所以请根据您的需要进行编辑!
基本上我有类似以下的情况:
class attributes(){
double xpos, ypos;
String id;
}
class item(){
String name;
int price;
List<attributes> attributeHistory;
Map<String, dynamic> toMap(){
Map<String, dynamic> returnMap = new Map();
returnMap['name'] = name;
returnMap['price'] = price;
returnMap['attributeHistory'] = //what goes here??;
}
}
那么......那里有什么?我已经为属性编写了一个 .toMap 函数,但我不知道这让我在哪里!
有任何想法吗?
最佳答案
创建 List
的 Map<String,dynamic>
returnMap['attributeHistory'] = attributeHistory.map((attribute) {
Map<String,dynamic> attributeMap = new Map<String,dynamic>();
attributeMap["id"] = attribute.id;
// same for xpos and ypos
return attributeMap;
}).toList();
关于dart - 如何使用自定义类对象列表为 Firestore 创建 map<String, Dynamic>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50825371/