Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Flutter cannot open a file: OS Error No such file or directory

ErrorFileSystemException: 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.

  • Applies to Flutter 3.x stable, Dart 3.x, path_provider current, Android and iOS
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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()}');
  2. 2Match what you see, then jump to its fix

    1. 1The 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. 2The first launch fails, later launches work after a saveThe file is read before anything has written itFix
    3. 3File('assets/data.json') fails, though the asset is declared in pubspec.yamlA bundled asset is opened as a normal fileFix

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 run

Confirmed by docs.flutter.dev

Did this fix it?

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 run

Confirmed by docs.flutter.dev

Did this fix it?

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.yaml

2Fix

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 run

Confirmed by docs.flutter.dev

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.