Abdalltif Basher
Abdalltif Basher

Reputation: 627

How to fetch contacts and print them using flutter?

I would like to fetch contacts and populate them in a listview, I read the flutter documentation:

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

How can I access the contact name?

Upvotes: 1

Views: 4151

Answers (3)

Dhruvrajsinh
Dhruvrajsinh

Reputation: 101

Use this flutter_contacts package In example, you'll find your solution with ease.

Upvotes: 0

Feisal  Aswad
Feisal Aswad

Reputation: 465

To get phone number ...this is my code:

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()
    }
}

Upvotes: 2

Yousuf AL-Mawali
Yousuf AL-Mawali

Reputation: 267

Contact is a class, and its fields are the following :

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;

you can access the contact name by iterating throw the array contacts , and access each fields , as the following :

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

Upvotes: 2

Related Questions