watchRelated<R extends DatumEntityBase> method

Stream<List<R>>? watchRelated<R extends DatumEntityBase>(
  1. T parent,
  2. String relationName
)

Reactively watches related entities for a given parent entity.

This method provides a stream of related entities that automatically updates when the underlying data changes.

  • parent: The entity instance for which to watch related data.
  • relationName: The name of the relation to watch.

Returns a Stream<List<R>> of the related entities, or null if the adapter does not support reactive queries. Throws an error if the relation is not defined.

Implementation

Stream<List<R>>? watchRelated<R extends DatumEntityBase>(
  T parent,
  String relationName,
) {
  _ensureInitialized();

  if (parent is RelationalDatumEntity) {
    final relation = parent.relations[relationName];
    if (relation == null) {
      throw Exception(
        'Relation "$relationName" is not defined on entity type ${parent.runtimeType}.',
      );
    }
  } else {
    throw ArgumentError(
      'The parent entity must be a RelationalDatumEntity to watch relations.',
    );
  }

  final relatedManager = Datum.manager<R>();

  return localAdapter.watchRelated(
    parent,
    relationName,
    relatedManager.localAdapter,
  );
}