insertContact static method

Future<Contact> insertContact(
  1. Contact contact
)

Inserts a new contact in the database and returns it.

Note that the output contact will be different from the input; for example the input can't have an ID, but the output will. If you intend to perform operations on the contact after creation, you should perform them on the output rather than on the input.

Implementation

static Future<Contact> insertContact(Contact contact) async {
  // This avoids the accidental case where we want to update a contact but
  // insert it instead, which would result in two identical contacts.
  if (contact.id.isNotEmpty) {
    throw Exception('Cannot insert contact that already has an ID');
  }
  if (!contact.isUnified) {
    throw Exception('Cannot insert raw contacts');
  }
  final json = await _channel.invokeMethod('insert', [
    contact.toJson(),
    config.includeNotesOnIos13AndAbove,
  ]);
  return Contact.fromJson(Map<String, dynamic>.from(json));
}