Convert/map Phone class model to another model with fewer properties only

50 Views Asked by At

I am fetching phone contacts where it gives the list of contacts. From the list I want only a few properties like display name, email and phone contacts only. How can we get only those properties from the contact list and ignore the other properties?

 List<Contact>? contactsList = await FlutterContacts.getContacts(
          withProperties: true, withPhoto: false);
1

There are 1 best solutions below

0
Jaimin Raval On BEST ANSWER

use below code.

late List contactsList;
late List<Contact> contacts;
@override
void initState() {
  _getContactPermission();
  super.initState();
}

void _getContactPermission() async {
  var status = await Permission.contact.request();
  if (status.isGranted) {
    contacts = await FlutterContacts.getContacts(
        withProperties: true, withPhoto: true);
    for (var userContact in contacts) {
      contactsList.add({
        'fname': userContact.name.first,
        'lname': userContact.name.last,
        'contact': contact.phones.isNotEmpty
            ? contact.phones.first.number
            : 'Not available'
      });
    }
    //contactsList use according your needs
  } else {
    // handle error
  }
}

i hope it will help you.