Flutter_Raj
Flutter_Raj

Reputation: 181

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

Upvotes: 3

Views: 2476

Answers (1)

Milvintsiss
Milvintsiss

Reputation: 1460

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"

Upvotes: 1

Related Questions