3

Uploading multiple images and waiting for the URL list, images were loaded to firestore successfully after few seconds and returns the url but before that the future function returns with null

Future<List<dynamic>> uploadImage() async {
     _imageFile.forEach((image) async {
      String rannum = Uuid().v1();
      StorageReference reference =
          FirebaseStorage.instance.ref().child('Reviews').child(rannum);
      StorageUploadTask uploadTask = reference.putFile(image);
      StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
      String _url = await downloadUrl.ref.getDownloadURL();
      _urllist.add(_url);
    });

    return _urllist;
  }

1 Answer 1

1

You have to call your function like this:

uploadImage(_imageFile).then((List<String> urls) => urls.forEach((element) => print(element)))

And I also changed your function:

Future<List<String>> uploadImage(List<File> _imageFile) async {
    List<String> _urllist = [];
    await _imageFile.forEach((image) async {
      String rannum = Uuid().v1();
      StorageReference reference =
      FirebaseStorage.instance.ref().child('Reviews').child(rannum);
      StorageUploadTask uploadTask = reference.putFile(image);
      StorageTaskSnapshot downloadUrl = await uploadTask.onComplete;
      String _url = await downloadUrl.ref.getDownloadURL();
      _urllist.add(_url);
    });

    return _urllist;
  }

also I don't understand why you enclosed with brackets "await uploadTask.onComplete"

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

8 Comments

Thanks, i tried your suggestion, it is not working. Still receiving blank list
Yes you have to store your download urls as String not Dynaminc, I updated my answer
I tried this option, still i am facing the same issue. On multiple image upload, the function is not waiting for all the download URLs. I have changed the function with future.delayed.
Can you post how you call this Future? Because it's a Future it's async and you have to await for the answer
I am using the .then as uploadImage().then((photourl) {}
|

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.