add<T extends RealmObject> method

T add<T extends RealmObject>(
  1. T object,
  2. {bool update = false}
)

Adds a RealmObject to the Realm.

This Realm will start managing the RealmObject. A RealmObject instance can be managed only by one Realm. If the object is already managed by this Realm, this method does nothing. This method modifies the object in-place as it becomes managed. Managed instances are persisted and become live objects. Returns the same instance as managed. This is just meant as a convenience to enable fluent syntax scenarios.

By setting the update flag you can update any existing object with the same primary key. Updating only makes sense for objects with primary keys, and is effectively ignored otherwise.

Throws RealmException when trying to add objects with the same primary key. Throws RealmException if there is no write transaction created with write.

Implementation

T add<T extends RealmObject>(T object, {bool update = false}) {
  if (object.isManaged) {
    _ensureManagedByThis(object, 'add object to Realm');

    return object;
  }

  final metadata = _metadata.getByType(object.runtimeType);
  final handle = _createObject(object, metadata, update);

  final accessor = RealmCoreAccessor(metadata, _isInMigration);
  object.manage(this, handle, accessor, update);

  return object;
}