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);

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

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

    handle = realmCore.createRealmObject(_realm, metadata.classKey);
  }

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