36

I was following - how to create directory from https://docs.flutter.io/flutter/dart-io/Directory-class.html

new Directory('dir/subdir').create(recursive: true)
  // The created directory is returned as a Future.
  .then((Directory directory) {
    print(directory.path);
});

This is the error I am getting:

FileSystemException: Creation failed, path = 'dir' (OS Error: Read-only file system, errno = 30)

I have enabled Storage(WRITE_EXTERNAL_STORAGE) permission. (Android device)

What am I missing?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
UpaJah
  • 6,954
  • 4
  • 24
  • 30

3 Answers3

38

Got it (Using path_provider plugin to get correct path)

Directory appDocDirectory = await getApplicationDocumentsDirectory();

new Directory(appDocDirectory.path+'/'+'dir').create(recursive: true)
// The created directory is returned as a Future.
    .then((Directory directory) {
  print('Path of New Dir: '+directory.path);
});
UpaJah
  • 6,954
  • 4
  • 24
  • 30
  • 1
    Is there a sycnhronous version of `Directory appDocDirectory = await getApplicationDocumentsDirectory();`? – Shiv Mar 28 '23 at 02:59
5

Update : New Flutter plugin permission_handler have taken care below thing so change your plugin to permission_handler

If you are testing in Android Oreo and want to write file in external storage then you need to ask WRITE_EXTERNAL_STORAGE

According to "https://developer.android.com/about/versions/oreo/android-8.0-changes.html#rmp"

If the app targets Android 8.0 (API level 26), the system grants only READ_EXTERNAL_STORAGE at that time; however, if the app later requests WRITE_EXTERNAL_STORAGE, the system immediately grants that privilege without prompting the user.

Permission Plugin ask only READ_EXTERNAL_STORAGE permission by Permission.requestPermissions([PermissionName.Storage]); so You need to ask for WRITE_EXTERNAL_STORAGE.

You can use my repo for asking WRITE_EXTERNAL_STORAGE in pubspec.yaml:

permission:
    git:
    url: https://github.com/kamlesh9070/permission
Kamlesh Kanazariya
  • 1,209
  • 2
  • 15
  • 32
0

If you have given the WRITE_EXTERNAL_STORAGE permission but still not able to create the directory,

you can try manually uninstalling the Flutter App from the device,

and rerun it.

NOTE: make sure after the installation, allow the storage permission from the app settings.

It works for me after struggling for hours (I don't know why this solution works)

SKJ
  • 237
  • 2
  • 11