0

I am trying to upload multiple images to Firebase storage. the issue I have is trying to retrieve the Url's for each of the images. the files itself upload successfully if I do not try to retrieve the Url's the issue i see happening is that the upload runs in parallel to the url's being generated and hence there isnt an update to the url by the time it gets to the second image.

the error i get is "[firebase_storage/object-not-found] No object exists at the desired reference."

Note: I am not using forEach because i am limiting the number of files to upload to 5. the code below is contained within a if statement to check the list length.

 for (var i = 0; i < _imageFileList!.length; i++) {

               if (kIsWeb) {

                var bytes = await _imageFileList![i].readAsBytes();

                print('photo$i');


                Reference snapshot =  await FirebaseStorage.instance.ref().child('items/$itemkey/photo$i');

                UploadTask uploadFile = snapshot.putData(bytes);

// If I delete the following 2 lines then the uploadof multiple files works without any issues.

                  imagesUrls.add(await (snapshot.getDownloadURL()));

                  print(imagesUrls);

/////    /////    /////    /////    /////    /////

              } else {

                String filePath = 'items/${itemkey}/photo${i}.jpg';

                Reference snapshot = FirebaseStorage.instance.ref().child(filePath);

                UploadTask uploadFile = snapshot.putFile(File(_imageFileList![i].path));

// currently doesnt work but testing on web so this doesnt trigger.

                final String downloadUrl = await snapshot.getDownloadURL();

                print(downloadUrl);

//////////////////////////////////
              }


            }

How do I get these to run async or await? i can't put await in front of the for loop because flutter tells me i cant use await for a normal for loop.

1 Answer 1

0

I was using the await function on the incorrect statement. In order to achieve the above the await should be on the next line of the upload task and that should be enclosed within a ".then" statement. below is the solution

Reference snapshot =  FirebaseStorage.instance.ref().child('items/$itemkey/photo$i');

                await snapshot.putData(bytes).then((value) async{

                  imagesUrls.add(await (snapshot.getDownloadURL()));
                  //
                  print(imagesUrls);


                });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.