当我要上传多张图片时,我遇到了问题。 我有一个包含“文件”的列表。我阅读了有关它的更多文章,并发现了这一点:
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;
}
但是当我调用此函数时:
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;
});
});
此功能有什么问题,有人可以帮我解决吗?
答案 0 :(得分:0)
根据我所做的事情,当用户从手机中选择照片或拍照时,它将把图像添加到List<Object> images'
中,这肯定您可能已经实现了自己。
关于上传这些图像,我所做的是将它成批上传到Firebase存储中,然后使用每个用户的url(如果您要使用的网址是我的代码)。
Future handleUploadImage() async {
var firebaseUser = await FirebaseAuth.instance.currentUser();
var uuid = new Uuid();
try {
for (int i = 0; i < widget.images.length; i++) {
final StorageReference storageRef =
FirebaseStorage.instance.ref().child(uuid.v4());
final StorageUploadTask task = storageRef
.child('user_images/${firebaseUser.uid}/')
.putFile(widget.images[i]);
await task.onComplete.then((picValue) async {
await picValue.ref.getDownloadURL().then((downloadUrl) {
print("URL : " + downloadUrl);
imageUrl.add(downloadUrl);
});
});
}
} catch (e) {
print(e);
}
}
请记住,我使用uuid软件包生成不同的图像URL