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

标签 dart google-cloud-firestore flutter

我正在尝试使用 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 函数,但我不知道这让我在哪里!

有任何想法吗?

最佳答案

创建 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();

关于dart - 如何使用自定义类对象列表为 Firestore 创建 map<String, Dynamic>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50825371/

相关文章:

dart - Angular.Dart JavaScript互操作错误

flutter - 如何将数据从表单传递到下一个屏幕?

dart - 如何在 Dart 中获取选定的文本或内部 html

flutter - 根据类型名称动态创建实例

android - Flutter-可以将边框半径赋予扩展面板列表吗?如果是,应该使用哪些属性

dart - 如何在 Angular Dart 中检索innerhtml

android - flutter ,云火存储查询方法,其中无法正常工作

javascript - 获取 Cloud Firestore 数据库集合

Firebase firestore复杂查询

flutter - 如何预缓存 flutter 火光动画