Defaults & Constants
Provide fallback values for null source fields and fixed constant values.
Defaults & Constants
Two @Mapping parameters let you inject values that don't come from the source object:
defaultValue:— used as the fallback when the source field isnullconstant:— always used, regardless of the source
Default Value
Provide a Dart expression string as the fallback for a nullable source field:
class UserInput {
final String name;
final String? status; // nullable
final String? bio; // nullable
const UserInput({required this.name, this.status, this.bio});
}
class UserOutput {
final String name;
final String status; // non-nullable
final String bio; // non-nullable
final String version;
const UserOutput({
required this.name,
required this.status,
required this.bio,
required this.version,
});
}
@Mapper()
abstract class UserMapper {
@Mapping(target: 'status', defaultValue: 'active')
@Mapping(target: 'bio', defaultValue: 'No bio provided')
@Mapping(target: 'version', constant: '1.0')
UserOutput toOutput(UserInput input);
}
Generated:
@override
UserOutput toOutput(UserInput input) {
return UserOutput(
name: input.name,
status: input.status ?? 'active',
bio: input.bio ?? 'No bio provided',
version: '1.0',
);
}
Constant Value
constant: always emits the given expression — the source field is not read:
@Mapping(target: 'apiVersion', constant: "'v2'")
ResponseDto toDto(ResponseData data);
Generated:
apiVersion: 'v2',
Because the value is emitted verbatim, string constants require inner quotes:
constant: "'active'"→ emits'active'constant: "42"→ emits42constant: "true"→ emitstrue
Difference: defaultValue vs constant
| Parameter | When applied | Source field read? |
|---|---|---|
defaultValue | Source field is null | Yes (checked for null) |
constant | Always | No |
Mutual Exclusion Rules
constantcannot be combined withsource,defaultValue, orcallabledefaultValuecannot be combined withcallable
See Also
- @Mapping annotation reference
- Conditional Expressions guide — condition-based fallback