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)Short answer
Errno 2 means the exact path you gave does not exist on the device. In Flutter this is usually a hard-coded path, a file that was never written, or an asset opened as if it were a file. Build the path from path_provider, create the file before reading it, and load bundled assets through rootBundle.
Fix it fast
1Find out which cause you have
Print the path and ask Dart whether it exists. The result separates a wrong path from a missing file.
final file = File(path); debugPrint('$path exists: ${await file.exists()}');2Match what you see, then jump to its fix
- 1
The path starts with a folder from your computer or a copied device pathThe path is typed in by hand instead of built from path_providerFix - 2
The first launch fails, later launches work after a saveThe file is read before anything has written itFix - 3
File('assets/data.json') fails, though the asset is declared in pubspec.yamlA bundled asset is opened as a normal fileFix
- 1
What does the error look like?
FileSystemException: Cannot open file, path = '...' (OS Error: No such file or directory, errno = 2)
Cause 1 of 3
The path is typed in by hand instead of built from path_provider
1Check
App storage folders differ per platform and per install. The Flutter recipe gets the documents directory from path_provider, which maps to NSDocumentDirectory on iOS and the app's data directory on Android.
grep -rn "File(" lib/2Fix
Ask path_provider for the directory and append the file name.
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/data.txt');3Verify
exists() prints true after the file is written.
flutter runConfirmed by docs.flutter.dev
Cause 2 of 3
The file is read before anything has written it
1Check
readAsString throws when the file is absent. The Flutter recipe wraps the read in try and catch and returns a default for exactly this case.
grep -rn "readAsString" lib/2Fix
Create the file with a write first, or catch the error and return a default value.
try {
return await file.readAsString();
} catch (_) {
return '';
}3Verify
A fresh install starts without the exception.
flutter runConfirmed by docs.flutter.dev
Cause 3 of 3
A bundled asset is opened as a normal file
1Check
Assets are packed into an asset bundle that the app reads at run time. Flutter's docs load them through an AssetBundle, never as files in dart:io.
grep -n -A4 "assets:" pubspec.yaml2Fix
Read the asset with rootBundle.loadString, using the key declared in pubspec.yaml.
import 'package:flutter/services.dart' show rootBundle;
final text = await rootBundle.loadString('assets/config.json');3Verify
The text loads without a file path.
flutter runConfirmed by docs.flutter.dev
How do I stop it coming back?
- Never store absolute device paths; store only the file name and rebuild the folder at run time.
- Check exists() or catch the error on every read.
- Use assets for read-only data and the documents directory for data the app changes.
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 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
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 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
image_picker closes the app or returns nothing after choosing a photo
With launchMode singleInstance the picker always returns RESULT_CANCELED
On Android the system can kill MainActivity while the picker is open, so the app restarts and the result never arrives.
3 causes, each with a fixMobile apps
Flutter debug app installs on an iPhone or iPad but the Dart VM never starts
EXC_BAD_ACCESS (SIGKILL - CODESIGNING)
On iOS 14 and later a debug build needs the Local Network permission for the Dart VM service, and it can only be started by Flutter tooling or Xcode, not by tapping its icon.
4 causes, each with a fix