Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I list all the paths and directories in my Firebase Storage with Flutter?

I'm using Flutter and in my Firebase Storage I have a complex architecture of files and directories. I would like to list all the files contained in Firebase Storage (including their path) in order to download them and stock them on the local storage of the device.

I know how to list all the files from a folder, but how do I list the files from all the folders at the same time without knowing the name of these folders?

For instance, my architecture could look like this:

MainFolder
  -- Folder2
    -- Folder3
      -- file.json
    -- Folder4
      -- file2.json
AnotherMainFolder
  -- file3.json

The structure in my Firebase Storage is dynamic (I do not know the name and the number of directories I have inside it).

Until now, I have managed to writke the following code:

class DownloadFirebaseApi {
  static Future<List<FirebaseFile>> listAll() async {
    // here, I should specify a path `ref(path)`
    // however I don't want to because
    // I want all the folders and all the files
    // no matter in which directory they are
    final ref = FirebaseStorage.instance.ref();
    final result = await ref.listAll();
    final urls = await _getDownloadLinks(result.items);
    return urls
        .asMap()
        .map((index, url) {
          final ref = result.items[index];
          final name = ref.name;
          final file = FirebaseFile(name: name, url: url, ref: ref);
          return MapEntry(index, file);
        })
        .values
        .toList();
  }

  static Future<List<String>> _getDownloadLinks(List<Reference> refs) async {
    return Future.wait(refs.map((ref) {
      var url = ref.getDownloadURL();
      return ref.getDownloadURL();
    }).toList());
  }
}

Is there a recursive method that I don't know for listAll()?

like image 593
ThomasG2201 Avatar asked Dec 08 '25 18:12

ThomasG2201


1 Answers

When you call listAll the result has two lists:

  1. result.items, which contains all individual files in the root.
  2. result.prefixes, which contains all directories (commonly referred to as "prefixes" in Cloud Storage).

You'll want to loop over result.prefixes too, and call listAll on each of those in turn to get the full, recursive list of files.

Also see the FlutterFire documentation on listing files and directories.

like image 87
Frank van Puffelen Avatar answered Dec 12 '25 03:12

Frank van Puffelen