android - 如何在flutter中将列表对象发布到cloud firestore firebase

标签 android ios firebase dart flutter-layout

我的列表为

class MyContact {
String userName = "";
List<Phone> phones = List<Phone>();
MyContact({this.userName, this.phones});
}

class Phone {
String phone = "";

Phone({this.phone});
}

及更新方法

  _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(????);
}

请帮助我!

最佳答案

我已解决如下

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

方法更新

_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);
}

关于android - 如何在flutter中将列表对象发布到cloud firestore firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58761308/

相关文章:

android - 尝试在设备 mako 的模拟器上启动 android 时出现错误 'ko:Invalid cache partition image type: yaffs2 (expected ext4)'?

android - 从 com.android.tools.build :gradle:3. 0.1 升级到 com.android.tools.build :gradle:3. 2.0

java - 如何在 Apache Commons Math 中执行整数线性规划

iOS:嵌入在 UIViewController 中的 Stripe 结帐 UI

ios - 在 Firebase 中注册 ios 应用程序时如何解决未知错误?

android import android-async-http-master 有错误

ios - 当新数据可用时如何防止使用缓存?

node.js - 从 Google App Engine 访问 Google Firebase 托管时出现 502 Bad Gateway 错误

reactjs - Firebase 错误 : Bucket name not specified or invalid

ios - swift 中的动画 : Circular Dashed Loading Bar?