简体   繁体   English

联系人更新在 flutter [contacts_service] 中不起作用

[英]Contact update not working in flutter [contacts_service]

I am using contact_service plugin to manage users' contact data in my flutter app.我在我的 flutter 应用程序中使用contact_service插件来管理用户的联系人数据。

i am getting 2 major issues while using this plugin我在使用这个插件时遇到了 2 个主要问题

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. 1.当我更新旧联系人时,没有出现错误,但手机号码已从旧联系人中删除,有时它会被删除。

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 2.我尝试使用循环一次保存多个联系人,所以我在循环中添加了联系人保存代码,如下所述

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.在这里,我遇到一个非常奇怪的问题,在这个循环中,我试图为大约 50 个人添加联系人,但是一个人的手机号码与另一个人的联系人结合在一起。 some times 3 or 4 different person's mobile numbers are saved in one contact.有时,一个联系人中会保存 3 或 4 个不同人的手机号码。

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 .否则尝试使用此插件代替flutter_contact

As per my understanding contact_service you initialise correctly with iOS & Android permissions as per mentioned in documentation.根据我的理解, contact_service您按照文档中提到的iOSAndroid权限正确初始化。 so i believe that you are getting contact correctly.still verify once.所以我相信你联系正确。仍然验证一次。

For contact update you must need to add identifier as per documentation.对于contact update ,您必须需要根据文档添加identifier also you need to use future loop for async tasks.您还需要将future循环用于async任务。

try like bellow code (tested in iOS):尝试像下面的代码(在 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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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