getBacklinks<T> method

RealmResults<T> getBacklinks<T>(
  1. String propertyName
)

Returns all the objects of type T that link to this object via propertyName. Example:

@RealmModel()
class School {
  late String name;
}

@RealmModel()
class Student {
  School? school;
}

// Find all students in a school
final school = realm.all<School>().first;
final allStudents = school.getBacklinks<Student>('school');

Implementation

RealmResults<T> getBacklinks<T>(String propertyName) {
  if (!isManaged) {
    throw RealmStateError("Can't look up backlinks of unmanaged objects.");
  }

  final sourceMeta = realm.metadata.getByType(T);
  final sourceProperty = sourceMeta[propertyName];

  if (sourceProperty.objectType == null) {
    throw RealmError("Property $T.$propertyName is not a link property - it is a property of type ${sourceProperty.propertyType}");
  }

  if (sourceProperty.objectType != realm.metadata.getByType(runtimeType).schema.name) {
    throw RealmError(
        "Property $T.$propertyName is a link property that links to ${sourceProperty.objectType} which is different from the type of the current object, which is $runtimeType.");
  }
  final handle = realmCore.getBacklinks(this, sourceMeta.classKey, sourceProperty.key);
  return RealmResultsInternal.create<T>(handle, realm, sourceMeta);
}