I'm facing the issue, when I want to upload multiple images. I have a List that holds the 'File'-s. I read more article about it, and found this:
Future<List> uploadImage(List<Object> _imageFile) async {
final user = await FirebaseAuth.instance.currentUser();
List _urllist = [];
int i = 0;
await _imageFile.forEach((image) async {
print('igen: ' + image.toString());
// if (ref.getMetadata() != null) { //should be deleted, but this isn't working
// await ref.delete();
// }
print(image.imageFile);
final ref = FirebaseStorage.instance
.ref()
.child('business_image')
.child(user.uid)
.child(i.toString() + '.jpg');
i++;
StorageUploadTask uploadTask = ref.putFile(image.imageFile);
StorageTaskSnapshot downloadUrl = await uploadTask.onComplete;
String _url = await downloadUrl.ref.getDownloadURL();
_urllist.add(_url);
});
print(_urllist);
return _urllist;
}
But when I'm calling this function:
List imageurllist;
final user = await FirebaseAuth.instance.currentUser();
uploadImage(getnewphotos).then((List urls) {
imageurllist = urls;
print(urls); //here I try to print it, but it returns null, guess the function don't wait for it?
Firestore.instance.collection('users').document(user.uid).updateData({
'email': widget.userData['email'],
'username': userName != null ? userName : widget.userData['username'],
'usertype': widget.userData['usertype'],
'loclat': loclat != null ? loclat : widget.userData['loclat'],
'loclng': loclng != null ? loclng : widget.userData['loclng'],
'locationread':
searchAddr != null ? searchAddr : widget.userData['locationread'],
'services': serviceList,
'opening': openstart != null ? openstart : widget.userData['opening'],
'closing': closeend != null ? closeend : widget.userData['closing'],
'userImage': imageurllist,
});
setState(() {
isUpload = false;
});
});
What is the problem with this function, can someone please help me solve it?