grabFile method

  1. @override
Future<List<FileData>?> grabFile(
  1. FilePickSettings settings
)
override

Implementation

@override
Future<List<FileData>?> grabFile(FilePickSettings settings) async {
  var files = await FilePicker.platform.pickFiles(
      allowedExtensions: settings.allowedExtensions,
      allowMultiple: settings.maxFileCount != 1);

  if (files == null) return null;

  if (settings.maxFileCount != null &&
      files.files.length > settings.maxFileCount!) {
    throw ("Max file count exceeded");
  }

  if (settings.maxFileSize != null &&
      files.files.any((e) => e.size > settings.maxFileSize!)) {
    throw ("Some files exceed max size (${convertByteToDisplay(settings.maxFileSize!)})");
  }

  if (files.files.any((e) => e.extension == null)) {
    throw "You have selected a curropted file: ${files.files.firstWhere((e) => e.extension == null).name}";
  }

  return [
    for (var file in files.files)
      FileData(
          name: file.name,
          data: await file.xFile.readAsBytes(),
          mimeType: file.extension!,
          size: file.size)
  ];
}