Flatter Fiebase数据库从Firebase存储获取图像url
我想从Firebase获取图像url并将其以字符串形式保存在CloudFireStore中。我可以将图像上传到FirebaseStore,但我无法将图像url作为字符串安全地上传到我的云Firestore 有人能帮我解决那个问题吗Flatter Fiebase数据库从Firebase存储获取图像url,firebase,flutter,firebase-realtime-database,dart,google-cloud-firestore,Firebase,Flutter,Firebase Realtime Database,Dart,Google Cloud Firestore,我想从Firebase获取图像url并将其以字符串形式保存在CloudFireStore中。我可以将图像上传到FirebaseStore,但我无法将图像url作为字符串安全地上传到我的云Firestore 有人能帮我解决那个问题吗 <!-- language: lang-dart --> //upload and get image File imageFile; String imageURL; Future getImage (bool isCamera) async
<!-- language: lang-dart -->
//upload and get image
File imageFile;
String imageURL;
Future getImage (bool isCamera) async {
File image;
if(isCamera) {
image = await ImagePicker.pickImage(source: ImageSource.camera);
}else{
image = await ImagePicker.pickImage(source: ImageSource.gallery);
}
setState(() {
imageFile = image;
});
}
Future uploadImage(BuildContext context) async {
String fileName = basename(imageFile.path);
StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(imageFile);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
setState(() {
imageURL = await taskSnapshot.ref.getDownloadURL();
});
}
//upload to realtime Database
RaisedButton(
color: Theme.of(context).primaryColor,
textColor: Colors.white,
child: Text(isEditNote? "Update" : "Save"),
onPressed: () async{
if(_key.currentState.validate() && imageFile != null){
try {
if(isEditNote){
uploadImage(context);
Book book = Book(
image: imageURL,
);
FirestoreService().updateBook(book);
Navigator.pop(context);
}else {
uploadImage(context);
Book book = Book(
image: imageURL,
);
await FirestoreService().addBook(book);
Navigator.pop(context);}
}catch(e){
print(e);
}
}else{
}
},
//上传并获取图像
文件图像文件;
字符串imageURL;
未来getImage(bool isCamera)异步{
文件图像;
如果(isCamera){
image=等待ImagePicker.pickImage(源:ImageSource.camera);
}否则{
image=等待ImagePicker.pickImage(源:ImageSource.gallery);
}
设置状态(){
图像文件=图像;
});
}
未来上载映像(BuildContext上下文)异步{
字符串fileName=basename(imageFile.path);
StorageReference firebaseStorageRef=FirebaseStorage.instance.ref().child(文件名);
StorageUploadTask uploadTask=firebaseStorageRef.putFile(imageFile);
StorageTaskSnapshot taskSnapshot=等待上载Task.onComplete;
设置状态(){
imageURL=await taskSnapshot.ref.getDownloadURL();
});
}
//上传到实时数据库
升起的按钮(
颜色:主题。背景。原色,
textColor:Colors.white,
子项:文本(isEditNote?“更新”:“保存”),
onPressed:()异步{
if(_key.currentState.validate()&&imageFile!=null){
试一试{
如果(请注意){
上传图像(上下文);
书(
图片:imageURL,
);
FirestoreService().updateBook(书籍);
Navigator.pop(上下文);
}否则{
上传图像(上下文);
书(
图片:imageURL,
);
等待FirestoreService().addBook(book);
Navigator.pop(上下文);}
}捕获(e){
印刷品(e);
}
}否则{
}
},
要获取图像URL
,可以将其设置为
imageUrl = await taskSnapshot.ref.getDownloadURL() ;
按定义
getDownloadURL()→ 将来
使用可撤销令牌异步检索长期存在的下载URL。这可用于与其他人共享文件,但如果需要,可由开发人员在Firebase控制台中撤销
在正式文件中:
我这样做就像我更改了代码一样,但我仍然无法将url作为字符串上传到云中