Skip to content

.length()

Passes when value.length equals length. Throws ArgumentError for a negative length.

StringValidator<O> length(int length, {String? message, MessageBuilder? messageBuilder})
Parameter Type Meaning
length int The only accepted length. params['length'].
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 invalidLength. Default message: value must be exactly 3 characters long. value is replaced by the field name when there is one.

@HasLength(n). Put the annotation on the constructor parameter or the field.

final iso = l.string().length(3);
iso.validate(input)+1-1
  1. valid: 'abc'
  2. invalid: 'abcd'value must be exactly 3 characters long