Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firebase upload multiple images

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;
  }
like image 431
Flutter_Raj Avatar asked Apr 10 '26 05:04

Flutter_Raj


1 Answers

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"

like image 193
Milvintsiss Avatar answered Apr 11 '26 18:04

Milvintsiss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!