android - 如何获得我用颤振上传到firebase的图片的精确参考
问题描述
这是我上传图片的代码
Future uploadProfileImage() async {
final StorageReference profileImageStorageRef =
FirebaseStorage.instance.ref().child("Profile Images");
var currentUserPicture = user.uid.toString();
final StorageUploadTask profileUploadTask = profileImageStorageRef
.child(currentUserPicture.toString() + ".jpg")
.putFile(myImage);
var imageUrl =
await (await profileUploadTask.onComplete).ref.getDownloadURL();
setState(() {
profilePicUrl = imageUrl.toString();
print("Image name = " + profilePicUrl);
});
}
它在我的 Firebase 数据存储中显示的只是“未来”的实例
解决方案
我没有立即看出代码中有什么问题,但是await
在一行中有两个语句使得很难快速扫描发生了什么。
我建议像这样重写它:
Future uploadProfileImage() async {
var profileImageStorageRef = FirebaseStorage.instance.ref().child("Profile Images");
var currentUserPicture = user.uid;
var useImageStorageRef = profileImageStorageRef.child(currentUserPicture + ".jpg")
var profileUploadTask = useImageStorageRef.putFile(myImage); // start upload in background
await profileUploadTask.onComplete; // wait for upload to complete
var imageUrl = await useImageStorageRef.getDownloadURL(); // wait for the download url
print("Image name = " + profilePicUrl);
setState(() {
profilePicUrl = imageUrl.toString();
});
}
推荐阅读
- c# - 映射表达式时,自动映射器出现错误
- php - 如何从某个字符后开始删除字符串
- java - 更新 SQLite 数据库并防止重置所有数据库
- aws-load-balancer - 无法在启动配置中选择 AWS 上的 Linux 2 AMI
- html - 如何在鼠标悬停时在 CSS 背景图像上创建缩小效果?
- css - CSS 网格媒体查询不起作用(初学者)
- python-3.x - 10以下的素数之和为2 + 3 + 5 + 7 = 17。求200万以下的所有素数之和
- android - React 中的 onClick 问题。台式机很好用,手机不行
- python - python cv2从皮肤蒙版3d数组图像中弹出黑色
- vba - 如何在空单元格中应用公式,否则什么都不做并移至下一张表