简体   繁体   中英

Contact update not working in flutter [contacts_service]

I am using contact_service plugin to manage users' contact data in my flutter app.

i am getting 2 major issues while using this plugin

1. when I am updating old contact, no errors are coming but the mobile number is getting removed from the old contact and sometimes its been getting deleted.

my code is like this

Contact? duplicateContact = <old contact>;
duplicateContact.givenName = <new name>;
duplicateContact.company = <company name>;
duplicateContact.phones = [Item(value: mobile, label: "mobile")];

await ContactsService.updateContact(contact);

so i this process is in loop as i want to update multiple contacts,but contact is not getting updated, instead of either mobile number field will be updated as empty or the whole contact will be get deleted.

so I don't know what is happening wrong with the contact update, I find out many things but nothing is working

2. I am trying save multiple contacts at a time using a loop, so I add the contact save code in loop as mention below

for (var element in contactData) {
    final contact = Contact(
          phones: [Item(value: <mobileNo>)],
          givenName: <name>,
          company: <companyName>,
        );
    await ContactsService.addContact(contact);
}

here, I am getting a very weird issue, In this loop, I am trying to add contacts for about 50 people, but one person's mobile number is combined with another person's contact. some times 3 or 4 different person's mobile numbers are saved in one contact.

Try to use:

await for (var element in contactData) {
    final contact = Contact(
          phones: [Item(value: <mobileNo>)],
          givenName: <name>,
          company: <companyName>,
        );
    ContactsService.addContact(contact);
}

I think you have forgotten some major steps to complete your code:

  1. Make sure you follow the steps from plugin page like Permissions.
  2. Take a look to an example might help you from question or from plugin page maybe you forgot to initializing some functionality.

Otherwise try to use this plugin instead flutter_contact .

As per my understanding contact_service you initialise correctly with iOS & Android permissions as per mentioned in documentation. so i believe that you are getting contact correctly.still verify once.

For contact update you must need to add identifier as per documentation. also you need to use future loop for async tasks.

try like bellow code (tested in iOS):

 import 'package:contacts_service/contacts_service.dart';
 
 // UPDATE / EDIT CONTACT
 static Future updateContact() async { 
    try {
      List<Contact> contactData = await ContactsService.getContacts();
      Contact editContact = Contact(
          givenName: "Petter",
          company: "Petter",
          phones: [Item(value: "+91 0000000000", label: "mobile")]);
    
      Future.forEach(contactData, (Contact contact) async { // Handle Async loop
        if (editContact.givenName == contact.givenName) {

          editContact.identifier = contact.identifier; // MUST ADD IDENTIFIER
    
          await ContactsService.updateContact(editContact); // UPDATE CONTACT

       // await ContactsService.deleteContact(editContact); // DELETE CONTACT
        }
      });
    } catch (ex) {
      "update contact error: $ex".printLog();
    }
 } 

// ADD MULTIPLE CONTACTS
Future addContact() async {
    List<Contact> contactData = [
      Contact(
          givenName: "Jackson",
          company: "Jackson",
          phones: [Item(value: "+91 1112223330", label: "mobile")]),
      Contact(
          givenName: "Jack",
          company: "Jack",
          phones: [Item(value: "+91 4445556660", label: "mobile")]),
      Contact(
          givenName: "Joseph",
          company: "Joseph",
          phones: [Item(value: "+91 7778889990", label: "mobile")]),
      Contact(
          givenName: "John",
          company: "John",
          phones: [Item(value: "+91 1234567890", label: "mobile")]),
      Contact(
          givenName: "Jayden",
          company: "Jayden",
          phones: [Item(value: "+91 0987654321", label: "mobile")]),
      Contact(
          givenName: "Petter",
          company: "Petter",
          phones: [Item(value: "+91 1212121212", label: "mobile")]),
    ];
    try {
      Future.forEach(contactData, (Contact contact) async { // Handle Async loop
        await ContactsService.addContact(contact);
      });
    } catch (ex) {
      debugPrint("add contact error: $ex");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM