firebase - 上传时ref.getDownloadURL为null,如何等待才能将其放入其他变量
问题描述
我正在尝试将图像上传到 firebase 存储,然后将下载 URL 放入 firebase 云 firestore,但数据库中的值始终为 null
File image;
String imageUrl;
...
Future<Null> uploadImage() async {
if (image == null) {
imageUrl = defaultImage;
} else {
StorageReference ref = FirebaseStorage.instance.ref().child(filename);
uploadTask = ref.putFile(image);
imageUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
}
}
void _addData() {
Firestore.instance.collection('kajian').add({
"imageUrl": imageUrl,
});
image = null;
imageUrl = null;
}
...
Scaffold(
floatingActionButton: new FloatingActionButton(
onPressed: () {
uploadImage();
_addData();
Navigator.pop(context);
},
child: Icon(Icons.cloud_upload),
),
解决方案
只有在图片上传完成后才能确定下载 URL。因此,您必须在上传完成后将其写入数据库。
Future<Null> uploadImage() async {
StorageReference ref = FirebaseStorage.instance.ref().child(filename);
uploadTask = ref.putFile(image);
String imageUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
Firestore.instance.collection('kajian').add({
"imageUrl": imageUrl,
});
}
推荐阅读
- javascript - 如何从 C# 服务器中的 POST(或其他 HTTP)请求访问参数?
- android - 迁移到 AndroidX 后,Gradle connectedCheck 失败并显示“未找到测试”
- python - Python Bs4:如何根据该行的特定“td”值检索表中的行
- c# - 从使用方法外部变量的不同类调用方法
- javascript - Oracle中的阿拉伯数字方向问题
- python - Django简单历史没有记录用户
- bash - 如何在第一个字符/下划线和重命名后去除文件名
- python-3.x - tkinter.filedialog.asksaveasfilename 保存文件慢
- python - 使单元格适合文本(并使其只读)
- reactjs - Microsoft Graph Toolkit 组件的事件处理程序未在 React 应用程序中调用