neil_ruaro
neil_ruaro

Reputation: 506

path_provider getExternalStorageDirectories() returns only one directory

I expected it to return a list of the various directories in the device's internal storage. Instead it returns the path to the app directory itself. I tried using for loops and forEach but both return only one directory.

void getFiles() async {
    var directories = await getExternalStorageDirectories();
    directories!.toList();
    for (var i = 0; i < directories.length; i++) {
      print(directories);
    }

    directories.forEach((element) {
      print(element);
    });
  }

I've also tried the following:

for (var i = 0; i < directories.length; i++) {
      print(directories[i].path);
}
for (var i = 0; i < directories.length; i++) {
      print(directories[i]);
}

All four return the same directory:

'/storage/emulated/0/Android/data/com.example.app/files'

How can I list all of the directories?

Upvotes: 1

Views: 970

Answers (1)

Maikzen
Maikzen

Reputation: 1624

If you want to get subdirectories of a directory do that:

List sublist = await Directory(directories[i].path).list().map((event) => event.path).toList();
      for (var j = 0; j < sublist.length; j++) {
        print(sublist[j]);
      }

Upvotes: 1

Related Questions