Flutter file write fails with Permission denied (errno = 13)
OS Error: Permission denied, errno = 13Short answer
Errno 13 means the operating system refused the file operation. In Flutter it usually means the path is outside the folders your app owns. Build the path from path_provider, such as the documents directory, instead of a hard-coded one. On macOS, the sandbox may also need a file entitlement.
Fix it fast
1Find out which cause you have
Print the exact path the failing call uses, and compare it with the app documents directory.
final dir = await getApplicationDocumentsDirectory(); print('app dir: ${dir.path}'); print('writing to: $yourPath');2Match what you see, then jump to its fix
What does the error look like?
OS Error: Permission denied, errno = 13
Cause 1 of 2
The path is outside the app's own storage
1Check
Android gives each app a private area that needs no permission. Apps that target Android 10 (API 29) or higher get scoped access to external storage by default, so writing to other locations is refused.
grep -rn "File(\|Directory(" lib/2Fix
Build the path from path_provider's application documents directory, as the Flutter file recipe does.
import 'dart:io';
import 'package:path_provider/path_provider.dart';
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/notes.txt');
await file.writeAsString('hello');3Verify
Run the write again and read the file back.
print(await file.readAsString());Confirmed by docs.flutter.dev
Cause 2 of 2
The macOS App Sandbox blocks the file access
1Check
macOS builds are sandboxed by default. Reading files the user picks, for example through the file_selector plugin, needs a user-selected entitlement in both entitlements files.
cat macos/Runner/DebugProfile.entitlements macos/Runner/Release.entitlements2Fix
Add the matching entitlement to both files, with identical changes in each, then rebuild.
<key>com.apple.security.files.user-selected.read-write</key>
<true/>3Verify
Rebuild the macOS app and repeat the file operation.
flutter run -d macosConfirmed by docs.flutter.dev
How do I stop it coming back?
- Never hard-code storage paths. Ask path_provider for the directory.
- Use the share or save-file picker when the user needs the file outside your app.
- On macOS, edit both entitlements files by hand and keep them in step.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
Flutter deep link works while the app is open but not from a closed app
From a fully closed app the link opens only the home screen
On a cold start Flutter does not push the link; it starts the app with an initial route that holds the link path.
3 causes, each with a fixMobile apps
Flutter request fails with SocketException: Failed host lookup
ClientException with SocketException: Failed host lookup
Failed host lookup means the app could not turn the host name into an IP address, so no request was sent.
4 causes, each with a fixMobile apps
Flutter Image.network shows nothing or throws when the image fails
EXCEPTION: resolving an image codec
Image.network throws an exception that goes to FlutterError.onError when a download or decode fails, so a 404 leaves a red screen or an empty box.
3 causes, each with a fixMobile apps
Flutter cannot open a file: OS Error No such file or directory
FileSystemException: Cannot open file, path = '...' (OS Error: No such file or directory, errno = 2)
Errno 2 means the exact path you gave does not exist on the device.
3 causes, each with a fixMobile apps
Flutter says Unable to load asset for an image or file that exists
The asset does not exist or has empty data.
Flutter only bundles files that pubspec.yaml lists.
4 causes, each with a fixMobile apps
Google Play says the app does not support 16 KB memory pages
check_elf_alignment.sh reports UNALIGNED for a library under lib/arm64-v8a
Google Play requires apps that target Android 15 or higher and ship native .so libraries to run on devices with 16 KB memory pages; from 1 February 2027 it will block updates that do not.
5 causes, each with a fix