@ModelField¶
Use @ModelField when a getter contains an embedded @Data or @Model value.
The annotation constructor is:
const ModelField({
String? name,
required Type referTo,
ModelFieldTemplate<Object?> template =
const ModelFieldTemplate<ModelFieldType>(),
})
nameis the stored field name. If omitted, the getter name is used.referToidentifies the annotated type represented by the field.templatedescribes the generated field shape when a specific model-field template is required.
ModelFieldTemplate<ModelFieldType> keeps one generated value. Use a template
with a nullable or collection-shaped ModelFieldType when the nested value has
that shape:
@ModelField(
name: 'profile',
referTo: _Profile,
template: ModelFieldTemplate<ModelFieldType?>(),
)
get profile;
@ModelField(
name: 'students',
referTo: _Student,
template: ModelFieldTemplate<List<ModelFieldType>>(),
)
List get students;
The generator substitutes the type referenced by referTo for
ModelFieldType. For the examples above, the generated shapes are equivalent
to Profile? and List<Student> respectively. A list of nested values is
represented inside the containing model; it does not create a separate
repository or relationship path for each nested value.
These templates are most useful when the storage format represents structured
JSON or another non-relational composite value. They describe the shape of a
nested field; they do not create relational foreign keys. Use
@ForeignField when the value should reference a separate
stored model.