我正在尝试获取从Firebase存储上载到Cloud Firestore的图像的下载链接。
成功打印了img的URL,但是在firebase控制台中,它给了我一个空值。
我尝试过多次实现此图片上传,希望能有所帮助。
String filename;
String url;
File image;
Future _getImage() async {
var _selectedImage =
await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
image = _selectedImage;
filename = basename(image.path);
});
}
Future<String> _saveImage() async {
StorageReference ref = FirebaseStorage.instance.ref().child(filename);
StorageUploadTask uploadTask = ref.putFile(image);
var downURL = await (await uploadTask.onComplete).ref.getDownloadURL();
url = downURL.toString();
print('LINK URL : $url');
return url;
}
void _addData() {
Firestore.instance.runTransaction((Transaction transaction) async {
CollectionReference reference = Firestore.instance.collection('test');
await reference.add({
'email': email,
'title': title,
'tags': tags,
'image': url,
});
});
}
答案 0 :(得分:0)
编写单独的函数以获取图像的网址
String url;
getURL() {
uploadTask.onComplete.then((s) async {
var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();
url = dowurl;
print(url);
if (url.isNotEmpty) {
return addData();
}
});
}