I have List as
class MyContact {
String userName = "";
List<Phone> phones = List<Phone>();
MyContact({this.userName, this.phones});
}
class Phone {
String phone = "";
Phone({this.phone});
}
and update method
_updateContact() async {
var contacts = (await ContactsService.getContacts()).toList();
List<MyContact> myContacts = List<MyContact>();
contacts.forEach((contact) {
var myContact = MyContact();
myContact.userName = contact.displayName;
contact.phones.forEach((phone) {
myContact.phones.add(Phone(phone: phone.value));
});
myContacts.add(myContact);
});
await Firestore.instance
.collection("contacts")
.document()
.setData(????);
}
Please help me!
I have resolved as follows
class MyContact {
String userName = "";
List<Phone> phones = List<Phone>();
MyContact({this.userName, this.phones});
Map<String, dynamic> toJson() {
return {
"name": userName,
"phones": phones.map((phone) => phone.toJson()).toList(),
};
}
}
class Phone {
String phone = "";
Phone({this.phone});
Map<String, dynamic> toJson() {
return {
"phone": phone,
};
}
}
Method update
_updateContact() async {
var contacts = (await ContactsService.getContacts()).toList();
List<MyContact> myContacts = List<MyContact>();
contacts.forEach((contact) {
List<Phone> phones = List<Phone>();
contact.phones.forEach((phone) {
phones.add(Phone(phone: phone.value));
});
myContacts.add(MyContact(userName: contact.displayName, phones: phones));
});
Map<String, dynamic> map = {
'contact': myContacts.map((myContact) => myContact.toJson()).toList()
};
await Firestore.instance
.collection("contacts")
.document()
.setData(map);
}
Flutter has official document for this master. It recommends to use "json_serializable" that the dart codes are generated automatically by command line
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.