Arish Khan
Arish Khan

Reputation: 750

How to get the root directory path in Flutter

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?

Upvotes: 13

Views: 18869

Answers (5)

Tariiq Dusmohamud
Tariiq Dusmohamud

Reputation: 53

Package external_path returns the path for top level storage.

First install the plugin:

flutter pub add external_path

Then import it:

import 'package:external_path/external_path.dart';

Finally run something like this:

var path = await ExternalPath.getExternalStorageDirectories();  // [/storage/emulated/0, /storage/B3AE-4D28] 2nd path refers to SD

Check out more here in the docs: https://pub.dev/documentation/external_path/latest/

Upvotes: 0

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

Upvotes: 3

Wali Khan
Wali Khan

Reputation: 652

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]
      }

Upvotes: 2

enzo
enzo

Reputation: 11496

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());

Upvotes: 2

Jaime
Jaime

Reputation: 388

The Directory class may have what you are looking for.

var myDir = new Directory('path_to_root');

Upvotes: -2

Related Questions