DEV Community

Gowthaman Ravichandran
Gowthaman Ravichandran

Posted on

Handling PathAccessException in iOS for File Download

In my Flutter application, I have implemented a feature that allows users to download a document and save it to a location of their choice. The process involves:

Using the FilePicker package to let the user select a directory.
Saving the file using File.writeAsBytes after the directory is selected.

This approach works perfectly on Android, but on iOS, I encounter a PathAccessException when attempting to save the file to the selected directory, likely due to platform-specific restrictions on file system access.

Below is the code for reference:

final String selectedDirectory = await FilePicker.platform.getDirectoryPath() ?? '';if (selectedDirectory.isNotEmpty) {
  final String filePath = '$selectedDirectory/$fileName$ext';
  final File file = File(filePath);
  await file.writeAsBytes(fileData);
}

What changes or permissions are required to enable writing files to a user-selected directory on iOS? Is there an alternative approach to achieve this functionality on iOS?

Top comments (0)