Reputation: 15
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 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
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
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
Reputation: 2415
There are 2 ways to access contacts in flutter.
Note: mobile_number plugin works for Android only because getting mobile number of sim card is not supported in iOS.
Upvotes: 0