我在firebase存储中存储了一个镜像,在Firestore中存储了镜像路径,如何使用Firestore中保存的路径获取该特定镜像?
getImagePath() async {
var document = await firestoreInstance
.collection("products")
.document(widget.productId)
.get();
String path = document["filePath"];
setState(() {
filePath = path;
});
}
我从Firestore获取的文件名是这样的,如何使用此文件名获取图像?
发布于 2020-08-21 13:05:02
我可以给你一个想法。既然您已经评论过您的filePath
看起来像这个image_picker4011381564582645700.jpg,,那么您必须只存储文件名。然后,您可以在firebase_storage的帮助下进行类似的操作
String getStorageUrl(String fileName) async {
String url = "";
StorageReference storageReference = FirebaseStorage.instance.ref().child("folder/$fileName");
url = await storageReference.getDownloadURL();
return url;
}
在您想要的地方调用该函数。或者对你来说,
getImagePath() async {
var document = await firestoreInstance
.collection("products")
.document(widget.productId)
.get();
String path = await getStorageUrl(document["filePath"]);
setState(() {
filePath = path;
});
}
然后,您可以在Image.network()
中使用该URL来显示它,或者,如果您有其他计划,可以使用flutter_downloader。
请确保使用正确的文件夹名称更改函数。希望这适合你的情况。
提示:最好将完整的URL存储到数据库,而不是只保存文件名。
发布于 2020-08-21 16:41:56
您也可以使用FirebaseImage小部件
CircleAvatar(
backgroundColor: Colors.black,
backgroundImage:
FirebaseImage('gs://yourapp=67791.appspot.com/images/cPW7zkbCpYQT2II7JqEOcUK5rOm2.png'),
)
发布于 2020-08-21 13:44:01
NetworkImage()
和Image.network()
直接从给定的Firebase镜像存储中获取镜像
//To set image on background
CircleAvatar(
backgroundColor: Colors.transparent,foregroundColor:Colors.blueAccent,
radius:16,
backgroundImage:NetworkImage(filepath),
child: FlatButton(
onPressed: (){
print("button pressed");},
),
),
//within child
Container(
child:Image.network(filepath),
);
https://stackoverflow.com/questions/63516719
复制相似问题