validateFile static method

bool validateFile({
  1. required File file,
  2. int? maxSizeInBytes,
  3. List<String>? allowedExtensions,
})

Validate file before upload

Implementation

static bool validateFile({
  required File file,
  int? maxSizeInBytes,
  List<String>? allowedExtensions,
}) {
  // Check if file exists
  if (!file.existsSync()) return false;

  // Check file size
  if (maxSizeInBytes != null) {
    if (file.lengthSync() > maxSizeInBytes) return false;
  }

  // Check file extension
  if (allowedExtensions != null && allowedExtensions.isNotEmpty) {
    final extension = file.path.split('.').last.toLowerCase();
    if (!allowedExtensions.contains(extension)) return false;
  }

  return true;
}