问题描述
我正在尝试保存从图库或照相机中拾取的图像.
Hi I am trying to save an image which is picked from gallery or camera.
Future<void> getImage({ImageSource source}) async {
_pickedImage = await ImagePicker().getImage(source: source);
if (_pickedImage != null) {
_pickedImageFile = File(_pickedImage.path);
}
}
拾取图像后,必须将图像更新到Firebase存储器中.
After picking the image it has to update image to the firebase storage.
void _uploadImage(File image) async {
Reference storageReference = FirebaseStorage.instance
.ref()
.child('UserImage')
.child("UserImage/${user.uid}");
UploadTask uploadTask = storageReference.putFile(image);
//StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
uploadTask.then((newUrl) async {
newUrl.ref.getDownloadURL();
return imageUrl = newUrl.toString();
}).catchError((onError) {
print(onError);
});
}
问题在于代码在uploadTask.then((newUrl)处中断,并且不允许以字符串形式获取downloadURL.然后,它必须将图像路径更新为firestore集合.
The issue is the code is breaking at uploadTask.then((newUrl) and not letting to get the downloadURL as a String. Then it has to update the image path to the firestore collection.
void userDetailsUpdate() {
FirebaseFirestore.instance
.collection("Manufacturer-Accounts")
.doc(user.uid)
.update({
'Designation': designation.text,
'FirstName': firstName.text,
'LastName': lastName.text,
//'Email': user.email,
'Phone': phoneNumber.text,
'UserImage': imageUrl == null ? "" : imageUrl,
});
}
因为没有得到图像URL,所以将值保存为".有办法吗?我已经使用了Firebase存储^ 5.0.1
Because it is not getting the image url so it is saving the value as "". Is there a way of doing it? I have used the Firebase storage ^5.0.1
我的完整源代码在此处给出
My full source code is given here How can I add a new value to my text editcontroller in Text formfield?
推荐答案
您的 _uploadImage
函数不会返回任何内容,因此,调用它的任何人都不会获得下载URL.
Your _uploadImage
function is not returning anything, so whoever calls it never gets the download URL.
Future<String> _uploadImage(File image) async {
Reference storageReference = FirebaseStorage.instance
.ref()
.child('UserImage')
.child("UserImage/${user.uid}");
UploadTask uploadTask = storageReference.putFile(image);
await Future.value(uploadTask);
var newUrl = await storageReference.getDownloadURL();
return newUrl;
}
现在您可以调用此函数并使用以下命令获取下载URL:
Now you can call this function and get the download URL with:
var downloadURL = await _uploadImage(file);
这篇关于如何将映像保存到Firebase存储并将URL保存到Firebase Firestore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!