Skip to content

l.list()

Passes when the value is a List and every element passes element. Each element’s issues are reported at the element’s index, so a bad second element appears at path 1, or tags.1 inside a schema. The output is a new List<E> of the validated elements, where E is the element validator’s output: l.list(l.int()) validates to List<int?> and l.list(l.int().required()) to List<int>. Use l.list(l.any()) to accept any elements and l.list(l.union([...])) for elements that may take several shapes.

ListValidator<E, List<E>?> list<E>(ValidatorReference<E> element, {String? message, MessageBuilder? messageBuilder})
Parameter Type Meaning
element ValidatorReference<E> Runs on every element. A forward reference is accepted.
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 invalidType. Default message: value must be a list when the value is not a list. Element failures carry the element validator’s own code, at the element’s index. value is replaced by the field name when there is one. Elements inherit the list’s field name.

A List<T>, Set<T> or Iterable<T> parameter maps to l.list(e) with e derived from T. A non-nullable T gets .required() inside the list; a nullable T? does not. Annotations on the field apply to the list itself, so @HasLength on a list fails generation. Element validators cannot be annotated yet.

final tags = l.list(l.string().required());
tags.validate(input)+2-2
  1. valid: ['a', 'b']
  2. valid: []
  3. invalid: ['a', 1, null]1: value must be a string2: value is required
  4. invalid: 'a'value must be a list

An optional element validator lets null elements through:

ValidationSuccess(data: [1, null])
ValidationFailure(errors: {1: [value is required]})