无法将存储 Firebase 映像正确关联到云还原



我不明白为什么我不能链接并发送URL到云消防商店集合文档该图像在存储中正确拾取,但无法在cloud firestore数据库中获取URL这是我的代码:


bool isLoading = false;
var firebaseUser =  FirebaseAuth.instance.currentUser;

Future uploadImage(BuildContext context) async {
String fileName = basename(_imageFile.path);
Reference firebaseStorageRef =
FirebaseStorage.instance.ref().child('uploads/$fileName');
UploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
TaskSnapshot taskSnapshot = await uploadTask.whenComplete(() => null);
if (taskSnapshot == null) {
final String downloadUrl =
await taskSnapshot.ref.getDownloadURL();
await FirebaseFirestore.instance.doc(firebaseUser.uid)
.collection("influncerUser")
.add({"imageUrl": downloadUrl,});
setState(() {
isLoading = true;
});
}
}

首先,为什么要使用if (taskSnapshot == null)?如果taskSnapshot为null,则无法获取下载url,因此请将其删除。

第二,更喜欢使用onComplete而不是whenComplete(((=>空(

Future uploadImage(BuildContext context) async {
String fileName = basename(_imageFile.path);
Reference firebaseStorageRef =
FirebaseStorage.instance.ref().child('uploads/$fileName');
UploadTask uploadTask = firebaseStorageRef.putFile(_imageFile);
// use onComplete to await when the doc has been pushed.
TaskSnapshot taskSnapshot = await uploadTask.onComplete;
final String downloadUrl = await taskSnapshot.ref.getDownloadURL();
await FirebaseFirestore.instance.doc(firebaseUser.uid)
.collection("influncerUser")
.add({"imageUrl": downloadUrl,});
setState(() => isLoading = true);

}

最新更新