DefaultValue class
DefaultValue is a metadata annotation primarily used by the code generation
framework (such as build_model) to define a fallback or initial value for
fields in a model class.
This annotation acts as a declarative instruction for the code generator, allowing it to automatically initialize properties in the generated implementation classes. This ensures that models always start in a well-defined state, even when created without explicit values for certain fields.
When to use
Use this on a field when you want a default value to be used if the field is not explicitly provided during construction. Common for optional fields.
How it works
The code generator reads this annotation and inserts the specified value into the constructor and/or field initializer of the generated class. The value must be a compile-time constant.
Examples
Primitives and Strings
@DefaultValue(42)
final int id;
@DefaultValue('Unnamed')
final String name;
@DefaultValue(true)
final bool isEnabled;
Enumerations
@DefaultValue(UserRole.guest)
final UserRole role;
Collections (Constants)
@DefaultValue(['standard'])
final List<String> permissions;
@DefaultValue({'theme': 'light'})
final Map<String, dynamic> config;
Parameters
- value: The literal value to be used as the default. Must be a compile-time constant.
Constructors
- DefaultValue(dynamic value)
-
Creates a DefaultValue metadata annotation.
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- value → dynamic
-
The literal value to be used as the default.
final
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited