Sangam Sharma
Sangam Sharma

Reputation: 15

Instance of <Contact> in Flutter

I'm using contacts_service: ^0.6.0 package, and I want to print the phone numbers from my phone book but it is printing Instance of < Contact > instead of actual phone number.

This is my phones screenshot

This is my output in debug panal.

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

This is my code:

`

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

Upvotes: 0

Views: 1128

Answers (3)

Aloysius Samuel
Aloysius Samuel

Reputation: 1200

Because you are printing the Instance itself, not the data inside them. So to print the data inside the Contact Instance you need to extract the data from it. For example:

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

Upvotes: 1

Subair K
Subair K

Reputation: 1880

Try this

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

Upvotes: 0

Divyesh
Divyesh

Reputation: 2415

There are 2 ways to access contacts in flutter.

  1. Using contact_service plugin
  2. Using mobile_number 1.0.4 plugin

Note: mobile_number plugin works for Android only because getting mobile number of sim card is not supported in iOS.

Upvotes: 0

Related Questions