Is there a plugin for this problem or can it be solved using dart:io
, because path_provider
does not have a method for accessing the root directory?

- 30,738
- 21
- 105
- 131

- 720
- 1
- 6
- 16
4 Answers
You can use this utility function:
import 'dart:io';
Directory findRoot(FileSystemEntity entity) {
final Directory parent = entity.parent;
if (parent.path == entity.path) return parent;
return findRoot(parent);
}
If you have a package such as path_provider
, you can use it like this in your code:
import 'package:path_provider/path_provider.dart';
final Directory root = findRoot(await getApplicationDocumentsDirectory());

- 9,861
- 3
- 15
- 38
-
FileSystemException: Directory listing failed, path = './' (OS Error: Permission denied, errno = 13) – Wali Khan Jul 31 '21 at 16:45
-
tried permission handler to get storage permission still doesn't work – Wali Khan Jul 31 '21 at 16:46
use this package https://pub.dev/packages/external_path to get root Directory of the device.
Future<void> getPath_1() async {
var path = await ExternalPath.getExternalStorageDirectories();
print(path); // [/storage/emulated/0, /storage/B3AE-4D28]
}

- 586
- 1
- 5
- 13
when we take path by:
- getApplicationDocumentsDirectory() method gives =
/data/user/0/com.crackhead.stacker/app_flutter/ and
- getExternalStorageDirectory() method gives =
/storage/emulated/0/Android/data/com.crackhead.stacker/files
soo here from 2nd one we see that internal storage path would be
/storage/emulated/0/
and for SD-card I had to hard-code seeing from Z-archiver app where path was:
/storage/4DC2-723F
and there corresponding directories would be:
Internal Storage : Directory('/storage/emulated/0/');
SD-card : Directory('/storage/4DC2-723F');
The Directory class may have what you are looking for.
var myDir = new Directory('path_to_root');

- 378
- 2
- 7
-
5each device will have different path bro, this is not an optimal solution – Arish Khan Jun 01 '20 at 15:19