Skip to content

Commit 6f493e1

Browse files
committed
Using string for files
1 parent 564c2f4 commit 6f493e1

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

lib/src/form_builder_validators.dart

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'dart:io';
2-
31
import 'package:flutter/material.dart';
42
import '../form_builder_validators.dart';
53

@@ -445,31 +443,31 @@ class FormBuilderValidators {
445443
: null;
446444

447445
/// [FormFieldValidator] that requires the field's value to be a valid file extension.
448-
static FormFieldValidator<File> fileExtension({
446+
static FormFieldValidator<String> fileExtension({
449447
required List<String> allowedExtensions,
450448
String? errorText,
451449
}) =>
452-
(File? valueCandidate) => valueCandidate == null
450+
(valueCandidate) => valueCandidate == null
453451
? null
454-
: !allowedExtensions.contains(
455-
fileExtensionFromPath(valueCandidate.path).toLowerCase())
452+
: !allowedExtensions
453+
.contains(fileExtensionFromPath(valueCandidate).toLowerCase())
456454
? errorText ??
457455
FormBuilderLocalizations.current
458456
.fileExtensionErrorText(allowedExtensions.join(', '))
459457
: null;
460458

461459
/// [FormFieldValidator] that restricts the size of an file to be less than or equal to the provided maximum size.
462460
/// * [maxSize] is the maximum size in bytes.
463-
static FormFieldValidator<File> fileSize({
461+
static FormFieldValidator<String> fileSize({
464462
required int maxSize,
465463
String? errorText,
466464
}) =>
467-
(File? valueCandidate) => valueCandidate == null
465+
(valueCandidate) => valueCandidate == null
468466
? null
469-
: valueCandidate.existsSync() && valueCandidate.lengthSync() > maxSize
467+
: int.parse(valueCandidate) > maxSize
470468
? errorText ??
471469
FormBuilderLocalizations.current.fileSizeErrorText(
472-
formatBytes(valueCandidate.lengthSync()),
470+
formatBytes(int.parse(valueCandidate)),
473471
formatBytes(maxSize),
474472
)
475473
: null;

test/form_builder_validators_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,9 @@ void main() {
592592
allowedExtensions: ['txt', 'pdf'],
593593
);
594594
// Pass
595-
expect(validator(File('test.txt')), isNull);
595+
expect(validator(File('test.txt').path), isNull);
596596
// Fail
597-
expect(validator(File('test.doc')), isNotNull);
597+
expect(validator(File('test.doc').path), isNotNull);
598598
}),
599599
);
600600

@@ -607,10 +607,10 @@ void main() {
607607
..createSync()
608608
..writeAsBytesSync(List.filled(512, 0));
609609
// Pass
610-
expect(validator(file), isNull);
610+
expect(validator(file.lengthSync().toString()), isNull);
611611
// Fail
612612
file.writeAsBytesSync(List.filled(2048, 0));
613-
expect(validator(file), isNotNull);
613+
expect(validator(file.lengthSync().toString()), isNotNull);
614614
// Cleanup
615615
file.deleteSync();
616616
}),

0 commit comments

Comments
 (0)