fileExtensionValidator static method

String? fileExtensionValidator(
  1. String? value,
  2. List<String> allowedExtensions, [
  3. String? errorMessage
])

fileExtensionValidator to validate a file path

validator: (value) => SimpleValidations.fileExtensionValidator(filePath, ['pdf', 'png'], [errorMessage]),

Implementation

static String? fileExtensionValidator(
    String? value, List<String> allowedExtensions,
    [String? errorMessage]) {
  if (value == null || value.isEmpty) {
    return errorMessage ?? 'Required';
  }

  String extension = value.split('.').last;
  if (!allowedExtensions.contains(extension.toLowerCase())) {
    return errorMessage ?? 'Invalid file extension';
  }
  return null;
}