flutter_contacts
flutter_contacts copied to clipboard
Not Displaying Contact Name properly
I have been working on an application that need to fetch user's contacts from device and display to contact name saved in device which works for some contacts but not for all contacts. It is displaying the contact name for some of the contacts and then it is displaying the contact number of some of the contacts. I am fetching and getting name by using the following code snippets.
/// Fetching Contacts
Future getContactsFromGloble() async {
allcontacts = [];
var getContacts = [];
var newgetContacts = [];
var contacts =
(await ContactsService.getContacts(withThumbnails: false)).toList();
allcontacts = contacts;
if (allcontacts != null) {
print("TOTAL:>>>>>>>>>>>" + allcontacts.length.toString());
for (int i = 0; i < allcontacts.length; i++) {
Contact? c = allcontacts.elementAt(i);
getContacts.add(c.phones?.map((e) =>
e.value?.replaceAll(new RegExp(r"\s+\b|\b\s"), "").toString()));
}
}
print('global');
return allcontacts;
}
/// Fetching contact name
getContactName(mobile) {
if (allcontacts != null && mobile != null) {
var name = mobile;
for (var i = 0; i < allcontacts.length; i++) {
if (allcontacts[i]
.phones!
.map((e) => e.value)
.toString()
.replaceAll(new RegExp(r"\s+\b|\b\s"), "")
.contains(mobile)) {
name = allcontacts[i].displayName;
}
}
return name;
} else {
return mobile;
}
}