如何使用自定义类对象列表为 Firestore 创建 map<String, Dynamic>?

Mik*_*Ups 4 dart flutter google-cloud-firestore

我正在尝试使用 firestore 为我的应用程序添加持久性。看起来不错,但我在路上遇到了一个颠簸,谷歌搜索似乎让我失望。我认为这可能比 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??;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么......那里有什么?我已经为属性编写了一个 .toMap 函数,但我不知道这让我在哪里!

有任何想法吗?

Tre*_*ree 12

创建ListMap<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();
Run Code Online (Sandbox Code Playgroud)