Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a map<String, Dynamic> for Firestore with Lists of custom class Objects?

I'm trying to add persistence to my app using firestore. It seems ace, but I've hit a bump in the road, and googling seems to be failing me. I think this may be more of a dart question than firestore, so please edit as you see fit!

Essentially I have a situation similar the following:

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??;
    }
}

So... What goes there? I've written a .toMap function for attributes, but I don't know where that gets me!

Any ideas?

like image 648
MikeCoverUps Avatar asked Dec 13 '22 16:12

MikeCoverUps


1 Answers

Create a List of 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();
like image 179
Tree Avatar answered Jan 19 '23 00:01

Tree