Reputation: 181
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
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