0

I need to upload a list of files to Firebase storage, so ive tried the following:

  Future uploadImage(List<File>? fileList) async {
    List<String> urlList = [];
    compressedImageFromDevice?.forEach((file) async {
      final ref = await FirebaseStorage.instance
          .ref()
          .child("img")
          .child(id)
          .child(time);
      final result = await ref.putFile(file);
      final String mediaUrl =
          await (await result.ref.getDownloadURL()).toString();
      urlList.add(mediaUrl);
      print(urlList.length()); //List contains an url
    });
    print(urlList.length()); //Outside of the loop the list keeps empty}
  }

Why is the list outside of the loop empty? Sorry but inserting code didn`t worked correctly.

julemand101
  • 28,470
  • 5
  • 52
  • 48

1 Answers1

-1

use for loop like below

for(var file in compressedImageFromDevice){

}

avoid forEach

erpExpert
  • 142
  • 1
  • 9