现状
我的 Firebase Storage 中有大约 200 个文件的文件夹。 在我的颤动中,我通过与文件名相同的密钥列表循环获取了文件夹中所有图片的下载链接。
Future getIconUrl(String name) async {
final ref = storageRef.child(name);
final url = await ref.getDownloadURL();
return url;
}
iconUrlFill(Map dict) async {
Map iconResult = {};
for (String key in dict.keys) {
var urlResult = await getIconUrl(key);
iconResult[key] = urlResult;
}
return iconResult;
}
iconUrlDict(Map dict) async {
Map result = await iconUrlFill(dict);
if(mounted){
setState(
() {
dataCount += 1;
urlDict = result;
},
);
}
}
问题 当前的方法需要大约 20 秒来加载所有下载网址,我想让它更快,因此我寻求可以提高性能的建议。
以下是如何更好地获取所有文件 URL。
Future<List<String>> getFilesUrl(String refPath) async {
try {
final ref = storage.ref().child(refPath);
final ListResult result = await ref.listAll();
final List<Reference> allFiles = result.items;
final List<String> urls = [];
for (Reference ref in allFiles) {
urls.add(await ref.getDownloadURL());
}
return urls;
} catch (e) {
debugPrint('Error getting files url: $e');
rethrow;
}
}