.required()
Adds one validation: the value is not null. Inside a schema, a missing key counts as null, so .required() is also what makes a field mandatory. It says nothing about emptiness: '' and [] pass. The return type drops the ?, so l.string().required() is a StringValidator<String> and success data is a String. It chains in any position. Optional by default explains the design.
Signature
Section titled “Signature”StringValidator<String> required({String? message, MessageBuilder? messageBuilder})// and the same on every other validator type, each returning its non-nullable formParameters
Section titled “Parameters”| Parameter | Type | Meaning |
|---|---|---|
message |
String? |
Replaces the default error message. |
messageBuilder |
MessageBuilder? |
Builds the message from the issue. message wins when both are given. See Message builders. |
Code required. Default message: value is required. value is replaced by the field name when there is one.
Code generation
Section titled “Code generation”The generator adds .required() to every parameter that is non-nullable, not dynamic, and has no default (a constructor default, @Default, or @JsonKey(defaultValue:)). Dart’s required keyword plays no part.
Example
Section titled “Example”final name = l.string().required();required String name, // l.string().required()required String? note, // l.string()@Default('en') String locale, // l.string()name.validate(input)+2-1- valid:
'Ada' - valid:
'' - invalid:
null