Search code examples
fluttergoogle-cloud-storagefirebase-storage

how to get download link of all files in folder of google storage in flutter?


Current Situation

I have folder of about 200 files inside Firebase Storage. enter image description here On my flutter I obtain download link to all the picture in the folder my loop through list of key that are same as file name.

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;
      },
    );
  }
}

Problem Current method take about 20second to load all the download url and I would like to make it faster and therefore I seek either advice that could make better performance.

  1. Way to obtain download link to all the files in the folder.
  2. better version of code above to increase performance.

Solution

  • Here is how you can get all file URLs in a better way.

      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;
        }
      }