How to get the root directory path in Flutter

Viewed 7323

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?

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

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

The Directory class may have what you are looking for.

var myDir = new Directory('path_to_root');

Related