Skip to content

l.file()

Passes when the value is a dart:io File (where dart:io exists), TypedData such as Uint8List, a ByteBuffer, a List<int>, or a Stream<List<int>>, or when accept returns true for it. The check is structural, so it behaves the same in minified web builds and a class that happens to be named UserProfile fails. A list decoded from JSON is a List<dynamic>, not a List<int>, and fails. null passes until you add .required().

FileValidator<Object?> file({FileAcceptor? accept, String? message, MessageBuilder? messageBuilder})
typedef FileAcceptor = bool Function(Object value);
Parameter Type Meaning
accept FileAcceptor? Accepts further values as files, such as (value) => value is XFile. An accept that throws rejects the value.
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 file. value is replaced by the field name when there is one.

A dart:io File or cross_file XFile parameter maps to l.file(). Any other declared type, including a package class such as MultipartFile that the generator knows nothing about, needs @IsFile() or the constant @isFile. The annotation replaces the type’s validator for the whole field, so a List<MultipartFile> field with @IsFile(accept: fn) generates one l.file(accept: fn) and fn receives the whole list. message and messageBuilder on the annotation pass through.

final upload = l.file();
upload.validate(input)+3-2
  1. valid: Uint8List.fromList([1, 2, 3])
  2. valid: <int>[1, 2, 3]
  3. valid: null
  4. invalid: 'avatar.png'value must be a file
  5. invalid: {'path': 'avatar.png'}value must be a file

accept extends the check to your own types:

true