<Contact> 在 Flutter 中的实例

Instance of <Contact> in Flutter

我正在使用 contacts_service: ^0.6.0 包,我想打印我的 phone 书中的 phone 号码,但它打印的是 的实例而不是实际的 phone个数.

This is my phones screenshot

这是我在调试面板中的输出。

I/flutter ( 8457): [Instance of 'Contact', Instance of 'Contact'].

这是我的代码:

`

_getContacts() async {
    List<Contact> _contacts =
    (await ContactsService.getContacts(withThumbnails: false)).toList();
    setState(() {
      contacts.addAll(_contacts);
      print(contacts);
    });
  }`

Flutter 中有两种访问联系人的方法。

  1. Using contact_service plugin
  2. Using mobile_number 1.0.4 plugin

注意:mobile_number插件仅适用于Android,因为iOS不支持获取sim卡手机号码。

试试这个

_getContacts() async {
    Future<List<Contact>> futureContacts = ContactsService.getContacts(withThumbnails: false)
        .then((value) => value.map((e) => Contact.fromMap(e.toMap())).toList());
    var contacts = await futureContacts.then((value) => value.map((e) => e.toMap()).toList());
    print(contacts);
  }

因为你打印的是实例本身,而不是其中的数据。因此,要打印联系人实例中的数据,您需要从中提取数据。例如:

_getContacts() async {
 List<Contact> _contacts =
 (await ContactsService.getContacts(withThumbnails: false)).toList();
 setState(() {
   contacts.addAll(_contacts);
   for(var i=0; i<contacts.length; i++){
     print(contacts[i].displayName);
   }
 });
}