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().
Signature
Section titled “Signature”FileValidator<Object?> file({FileAcceptor? accept, String? message, MessageBuilder? messageBuilder})
typedef FileAcceptor = bool Function(Object value);Parameters
Section titled “Parameters”| 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.
Code generation
Section titled “Code generation”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.
Example
Section titled “Example”final upload = l.file();@IsFile() required Object avatar, // l.file().required()@IsFile(accept: isMultipart) required MultipartFile upload, // l.file(accept: isMultipart).required()upload.validate(input)+3-2- valid:
Uint8List.fromList([1, 2, 3]) - valid:
<int>[1, 2, 3] - valid:
null - invalid:
'avatar.png' - invalid:
{'path': 'avatar.png'}
accept extends the check to your own types:
true