updateContact static method

Future<Contact> updateContact(
  1. Contact contact
)

Updates existing contact and returns it.

Note that output contact may be different from the input. If you intend to perform operations on the contact after update, you should perform them on the output rather than on the input.

Implementation

static Future<Contact> updateContact(Contact contact) async {
  // This avoids the accidental case where we want to insert a contact but
  // update it instead, which won't work.
  if (contact.id.isEmpty) {
    throw Exception('Cannot update contact without ID');
  }
  // In addition, on Android we need a raw contact ID.
  if (Platform.isAndroid &&
      !contact.accounts.any((x) => x.rawId.isNotEmpty)) {
    throw Exception('Cannot update contact without raw ID on Android');
  }
  // This avoids the accidental case where we try to update a contact before
  // fetching all their properties or photos, which would erase the existing
  // properties or photos.
  if (!contact.propertiesFetched || !contact.photoFetched) {
    throw Exception(
        'Cannot update contact without fetching properties and photos');
  }
  if (!contact.isUnified) {
    throw Exception('Cannot update raw contacts');
  }
  final json = await _channel.invokeMethod('update', [
    contact.toJson(),
    config.includeNotesOnIos13AndAbove,
  ]);
  return Contact.fromJson(Map<String, dynamic>.from(json));
}