将图像上传到firebase存储,然后将其写入firestore
我正在制作一个程序,每当用户注册时,他们都需要将图像上载到firebase存储,然后在图像上载后,所有登录数据和图像url都将写入firestore,这是我的代码:将图像上传到firebase存储,然后将其写入firestore,firebase,flutter,google-cloud-firestore,firebase-storage,Firebase,Flutter,Google Cloud Firestore,Firebase Storage,我正在制作一个程序,每当用户注册时,他们都需要将图像上载到firebase存储,然后在图像上载后,所有登录数据和图像url都将写入firestore,这是我的代码: void signUp(String _email, String _password, File image) async { await _auth .createUserWithEmailAndPassword(email: _email, password: _password) .then((r
void signUp(String _email, String _password, File image) async {
await _auth
.createUserWithEmailAndPassword(email: _email, password: _password)
.then((result) async{
await uploadImage(image).then((value) {
_firestore.collection('users').doc(result.user.uid).set({
'uid': result.user.uid,
'email': result.user.email,
'imageUrl' : value!=null?value:""
});
});
Future<String> uploadImage(File image) async{
String fileName = p.basename(image.path);
Reference storageRef = _fstorage.ref().child('upload/$fileName');
UploadTask uploadTask = storageRef.putFile(image);
await uploadTask.whenComplete((){
storageRef.getDownloadURL().then((imageUrl) {
return imageUrl!=null?imageUrl:"";
});
});
}
void注册(字符串\u电子邮件、字符串\u密码、文件图像)异步{
等待授权
.createUserWithEmailAndPassword(电子邮件:\电子邮件,密码:\密码)
.then((结果)异步{
等待上载映像(映像)。然后((值){
_firestore.collection('users').doc(result.user.uid).set({
“uid”:result.user.uid,
“电子邮件”:result.user.email,
“imageUrl”:值!=null?值:“
});
});
未来上载映像(文件映像)异步{
字符串文件名=p.basename(image.path);
Reference storageRef=_fstorage.ref().child('upload/$fileName');
UploadTask UploadTask=storageRef.putFile(图像);
等待上载任务。完成时(){
storageRef.getDownloadURL()。然后((imageUrl){
返回imageUrl!=null?imageUrl:;
});
});
}
问题是firestore文档上写入的imageUrl值始终为空,即使图像文件已成功上载,任何建议都将不胜感激。感谢函数
uploadImage
实际上并没有向调用方返回任何内容。您现在的返回语句只是从匿名文件返回一个值函数在完成时传递给。不会从顶级函数传播出去。在编写之前,请尝试打印value
的值以查看它实际包含的内容
似乎您只想依次等待putFile
和getDownloadURL
的结果,以获取URL,然后返回它
Future<String> uploadImage(File image) async{
String fileName = p.basename(image.path);
Reference storageRef = _fstorage.ref().child('upload/$fileName');
await storageRef.putFile(image);
return await storageRef.getDownloadURL()
}
未来上载映像(文件映像)异步{
字符串文件名=p.basename(image.path);
Reference storageRef=_fstorage.ref().child('upload/$fileName');
等待storageRef.putFile(图像);
return wait-storageRef.getDownloadURL()
}
我还建议查看。函数uploadImage
实际上没有向调用方返回任何内容。您现在使用的return语句只是在完成时从传递给的匿名函数返回一个值。它不会从顶级函数传播出去。请尝试打印值的值e> 在你写之前先看看它到底包含了什么
似乎您只想依次等待putFile
和getDownloadURL
的结果,以获取URL,然后返回它
Future<String> uploadImage(File image) async{
String fileName = p.basename(image.path);
Reference storageRef = _fstorage.ref().child('upload/$fileName');
await storageRef.putFile(image);
return await storageRef.getDownloadURL()
}
未来上载映像(文件映像)异步{
字符串文件名=p.basename(image.path);
Reference storageRef=_fstorage.ref().child('upload/$fileName');
等待storageRef.putFile(图像);
return wait-storageRef.getDownloadURL()
}
我还建议查看。未来的uploadItemImage(mFileImage)异步{
最终引用storageReference=
FirebaseStorage.instance.ref().child(“项”);
TaskSnapshot TaskSnapshot=等待
storageReference.child(“product_u$productId.jpg”)
.putFile(mFileImage);
var downloadUrl=await taskSnapshot.ref.getDownloadURL();
返回下载URL;
}
未来上传项目映像(mFileImage)异步{
最终引用storageReference=
FirebaseStorage.instance.ref().child(“项”);
TaskSnapshot TaskSnapshot=等待
storageReference.child(“product_u$productId.jpg”)
.putFile(mFileImage);
var downloadUrl=await taskSnapshot.ref.getDownloadURL();
返回下载URL;
}
上传后是否尝试打印值?是否返回url?上传后是否尝试打印值?是否返回url?谢谢!谢谢