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