pickFile method

Future<File?> pickFile({
  1. List<String>? allowedExtensions,
  2. FileType type = fp.FileType.any,
  3. bool allowCompression = true,
  4. bool allowMultiple = false,
  5. String? dialogTitle,
})

Implementation

Future<File?> pickFile({
  List<String>? allowedExtensions,
  fp.FileType type = fp.FileType.any,
  bool allowCompression = true,
  bool allowMultiple = false,
  String? dialogTitle,
}) async {
  bool hasPermission = await _checkStoragePermission();
  if (!hasPermission) return null;

  try {
    final result = await fp.FilePicker.platform.pickFiles(
      type: allowedExtensions != null ? fp.FileType.custom : type,
      allowedExtensions: allowedExtensions,
      allowCompression: allowCompression,
      allowMultiple: allowMultiple,
      dialogTitle: dialogTitle,
      withData: false,
      withReadStream: true,
    );

    if (result != null && result.files.isNotEmpty) {
      final path = result.files.first.path;
      if (path != null) {
        return File(path);
      }
    }
  } catch (e) {
    print('Error picking file: $e');
  }
  return null;
}