【发布时间】:2021-12-23 21:10:52
【问题描述】:
我正在使用 Flutter,在我的 Firebase 存储中,我有一个复杂的文件和目录架构。我想列出 Firebase 存储中包含的所有文件(包括它们的路径),以便下载它们并将它们存储在设备的本地存储中。
我知道如何列出一个文件夹中的所有文件,但是如何在不知道这些文件夹名称的情况下同时列出所有文件夹中的文件?
例如,我的架构可能如下所示:
MainFolder
-- Folder2
-- Folder3
-- file.json
-- Folder4
-- file2.json
AnotherMainFolder
-- file3.json
我的 Firebase 存储中的结构是动态的(我不知道其中的目录名称和数量)。
到目前为止,我已经设法编写了以下代码:
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());
}
}
对于listAll(),有我不知道的递归方法吗?
【问题讨论】:
标签: firebase flutter firebase-storage