Query<InstanceType extends ManagedObject>.forEntity constructor Null safety

Query<InstanceType extends ManagedObject>.forEntity(
  1. ManagedEntity entity,
  2. ManagedContext context
)

Creates a new Query without a static type.

This method is used when generating queries dynamically from runtime values, where the static type argument cannot be defined. Behaves just like the unnamed constructor.

If entity is not in context's ManagedContext.dataModel, throws a internal failure QueryException.

Implementation

factory Query.forEntity(ManagedEntity entity, ManagedContext context) {
  if (!context.dataModel!.entities.any((e) => identical(entity, e))) {
    throw StateError(
      "Invalid query construction. Entity for '${entity.tableName}' is from different context than specified for query.",
    );
  }

  return context.persistentStore.newQuery<InstanceType>(context, entity);
}