如何获取联系人并使用flutter打印它们?

时间:2019-11-05 17:29:39

标签: android ios flutter

我想获取联系人并将其填充到列表视图中,我阅读了flutter文档:

// Get all contacts on device
Iterable<Contact> contacts = await ContactsService.getContacts();  

如何访问联系人姓名?

2 个答案:

答案 0 :(得分:1)

联系人是一门课程,其字段如下:

String displayName, givenName, middleName, prefix, suffix, familyName;

// Company
String company, jobTitle;

// Email addresses
Iterable<Item> emails = [];

// Phone numbers
Iterable<Item> phones = [];

// Post addresses
Iterable<PostalAddress> postalAddresses = [];

// Contact avatar/thumbnail
Uint8List avatar;

您可以通过以下方式访问联系人名称:反复抛出数组联系人,并访问每个字段,如下所示:

contacts.forEach((contact){
      print(contact.displayName);
});

答案 1 :(得分:1)

要获取电话号码...这是我的代码:

import 'package:contacts_service/contacts_service.dart';
import 'package:permission_handler/permission_handler.dart';

void printContactsNumber() {
    Iterable<Contact> contacts = await ContactsService.getContacts(withThumbnails: false);
    List<Contact> contactsList = contacts.toList();

    // now print first 50 number's contancts
    for (int i = 0; i < 50; i++) {
        contactsList[0].phones.first.value.toString()
    }
}