12

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arish Khan
  • 720
  • 1
  • 6
  • 16

4 Answers4

2

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());
enzo
  • 9,861
  • 3
  • 15
  • 38
2

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]
      }
Wali Khan
  • 586
  • 1
  • 5
  • 13
1

when we take path by:

  1. getApplicationDocumentsDirectory() method gives =

/data/user/0/com.crackhead.stacker/app_flutter/ and

  1. 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');

enter image description here

-3

The Directory class may have what you are looking for.

var myDir = new Directory('path_to_root');

Jaime
  • 378
  • 2
  • 7