updateContact static method

Future<Contact> updateContact(
  1. Contact contact, {
  2. bool withGroups = false,
})

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, {
  bool withGroups = false,
}) 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, make sure to '
        'specify `withAccounts: true` when fetching contacts');
  }
  // 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 properties and photo, make sure to '
        'specify `withProperties: true` and `withPhoto: true` when fetching '
        'contacts');
  }
  if (!contact.isUnified) {
    throw Exception('Cannot update raw contacts');
  }
  final json = await _channel.invokeMethod('update', [
    contact.toJson(),
    withGroups,
    config.includeNotesOnIos13AndAbove,
  ]);
  return Contact.fromJson(Map<String, dynamic>.from(json));
}