create method

RealmObject create(
  1. String className, {
  2. Object? primaryKey,
})

Creates a managed RealmObject with the specified className and primaryKey.

Implementation

RealmObject create(String className, {Object? primaryKey}) {
  final metadata = _realm._metadata.getByName(className);

  ObjectHandle handle;
  if (metadata.primaryKey != null) {
    if (primaryKey == null) {
      throw RealmError("The class $className has primary key defined, but you didn't pass one");
    }

    handle = _realm._handle.createWithPrimaryKey(metadata.classKey, primaryKey);
  } else {
    if (primaryKey != null) {
      throw RealmError("The class $className doesn't have primary key defined, but you passed $primaryKey");
    }

    handle = _realm._handle.create(metadata.classKey);
  }

  final accessor = RealmCoreAccessor(metadata, _realm._isInMigration);
  return RealmObjectInternal.create(RealmObject, _realm, handle, accessor) as RealmObject;
}