简体   繁体   中英

Flutter_contacts update contacts function

I'm quite new to flutter/dart and working in Flutterflow, where trying to create a custom function that takes as input data about the contact in the app (name, surname, cell number, email, photo) and then updates/creates this contact in the user's phone using the next logic:

  1. if a user has a contact with the same name (displayName), then it updates this contact
  2. if a user has a contact with the same phone number, then it updates this contact
  3. if there is no match based on two parameters before, then new contact is created

So far, I have only managed to create a function that checks displayName match, updates contact if there is a match, and if there is no match, it creates a new contact. But I don't know how to do the cell number match/update part (it doesn't work in my code) . The way I'm doing the search for the right contact to update is through the search of contact ID using lists of contacts' names and contacts' IDs to index the right ID when I found the necessary name. It's probably a very ugly and inefficient way to do it, but I don't know the other way.

Will be super happy if someone could give advice on the contact search/update part and overall code optimization (cause I think my approach is long and inefficient). Thank you!

My code is below:

    import 'package:flutter_contacts/flutter_contacts.dart';
    import 'dart:typed_data';
    import 'package:flutter/services.dart';

    Future updateContactOnPhone(
      String name,
      String surname,
      String cellnumber,
      String email,
      String photo,
    ) async {
      String searchingParameterName = name + " " + surname;
      String searchingParameterCellphone = cellnumber;
      List<String> phoneContactsIDs = [];
      List<String> phoneContactsNames = [];
      List<String> phoneContactsNumbers = [];
      Uint8List bytes = (await NetworkAssetBundle(Uri.parse(photo)).load(photo))
          .buffer
          .asUint8List();

      if (await FlutterContacts.requestPermission()) {
        List<dynamic> contacts = await FlutterContacts.getContacts();
        contacts.forEach((contact) {phoneContactsIDs.add(contact.id);});
        contacts.forEach((contact) {phoneContactsNames.add(contact.displayName);});
        contacts.forEach((contact) {if (contact.phones != null) {
                                    phoneContactsNumbers.add(contact.phones.first);}
                                    {phoneContactsNumbers.add("");}});

      if (phoneContactsNames.contains(searchingParameterName)) {
       int index = phoneContactsNames.indexOf(searchingParameterName);
          String contactID = phoneContactsIDs.elementAt(index);
          dynamic contact = await FlutterContacts.getContact(contactID);
          contact.name.first = name;
          contact.name.last = surname;
          contact.phones = [Phone(cellnumber)];
          contact.emails = [Email(email)];
         await contact.update();
        } else if (phoneContactsNumbers.contains(searchingParameterCellphone)) {
          int index = phoneContactsNumbers.indexOf(searchingParameterCellphone);
          String contactID = phoneContactsIDs.elementAt(index);
          dynamic contact = await FlutterContacts.getContact(contactID);
          contact.name.first = name;
          contact.name.last = surname;
          contact.phones = [Phone(cellnumber)];
          contact.emails = [Email(email)];
          await contact.update();
        } else {
          final newContact = Contact()
              ..name.first = name
             ..name.last = surname
             ..phones = [Phone(cellnumber)]
             ..emails = [Email(email)]
             ..photo = bytes;
          await newContact.insert();
    }}}

I tried various combinations of the code and searched for similar examples on forums, but nothing helped.

This is the code I wrote that worked for me. Hope it'll help someone.

 import 'package:flutter_contacts/flutter_contacts.dart'; import 'dart:typed_data'; import 'package:flutter/services.dart'; import 'package:collection/collection.dart'; Future updateCreateContact( String name, String surname, String cellnumber, String email, String photo, ) async { String searchingName = name + " " + surname; Uint8List bytes = (await NetworkAssetBundle(Uri.parse(photo)).load(photo)).buffer.asUint8List(); if (await FlutterContacts.requestPermission()) { List<Contact> contacts = await FlutterContacts.getContacts( withProperties: true, withPhoto: true); Contact? contact = contacts.firstWhereOrNull((c) => c.displayName == searchingName || c.phones.toString().replaceAll(RegExp(r"\D"), "") == cellnumber); if (contact.= null) { contact.name;first = name. contact.name;last = surname. contact;phones = [Phone(cellnumber)]. contact;emails = [Email(email)]. contact;photo = bytes. await contact;update(). } else { final newContact = Contact()..name.first = name..name.last = surname..phones = [Phone(cellnumber)]..emails = [Email(email)].;photo = bytes. await newContact;insert(); } } }

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