(颤音)如何在Firestore中存储的路径中检索firebase存储图像
在我的代码中,我有一个streambuilder
,该使用geoflutterfire
软件包从firestore返回项目,以加载用户半径内的项目。
这些物品已加载,以及从Firestore检索的食物标题,过敏和年龄。除了提到的字段外,还有一个ImagePath
字段。
图像路径是test
firebase Storage中的文件夹中的一个子信息:
现在,有一个占位符图像,图像,图像MacBook,在我希望自己的图像的位置。由于查询与名称,过敏等一起加载图像路径,是否可以在流构建器内部显示其图像路径?这是查询
和streambuilder
的代码。
查询:
var collectionReference = FirebaseFirestore.instance.collection('requests');
double radius = 1000;
String field = 'position';
Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
.within(center: center, radius: radius, field: field);
streambuilder :(滚动到//占位符代码图像
查找占位符图像)
StreamBuilder(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
if (snapshots.connectionState == ConnectionState.active &&
snapshots.hasData) {
return ListView(
physics: const BouncingScrollPhysics(),
children: snapshots.data!.map(
(DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: const Color(0xff303434),
),
child: Row(
children: [
//CODE OF PLACEHOLDER IMAGE
Padding(
padding: const EdgeInsets.all(15.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(40.0),
child: Image.network(
'https://picsum.photos/250?image=9',
fit: BoxFit.fill,
width: 150,
height: 150,
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: 140,
),
child: AutoSizeText(
data['foodCategory'].toString(),
maxLines: 1,
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 25,
fontWeight: FontWeight.w900,
),
),
),
const SizedBox(
height: 5,
),
ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: 70.0,
maxWidth: 140,
),
child: AutoSizeText(
data['allergens'],
minFontSize: 10,
maxFontSize: 25,
style: GoogleFonts.poppins(
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(
height: 5,
),
AutoSizeText(
data['ageNum'].toString() +
' ' +
data['ageUnit'].toString(),
maxLines: 1,
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w400,
),
),
],
),
],
),
);
},
).toList(),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
In my code, I have a streambuilder
that returns items from Firestore using the geoflutterfire
package to load items that are within a radius of the user.
These items are loaded, along with the food title, allergies, and age which are retrieved from Firestore. Along with the mentioned fields, there is also an imagePath
field.
The image path is a subItem in the test
folder in Firebase Storage:
Right now, there is a placeholder image, the image of the MacBook, over the spot where I want my image to be. Since the query loads the image path along with the name, allergies, etc, is it possible to show an image given its image path inside of a streambuilder? Here is the code for the query
and streambuilder
.
Query:
var collectionReference = FirebaseFirestore.instance.collection('requests');
double radius = 1000;
String field = 'position';
Stream<List<DocumentSnapshot>> stream = geo.collection(collectionRef: collectionReference)
.within(center: center, radius: radius, field: field);
Streambuilder: (Scroll to //CODE OF PLACEHOLDER IMAGE
to find placeholder image)
StreamBuilder(
stream: stream,
builder: (BuildContext context,
AsyncSnapshot<List<DocumentSnapshot>> snapshots) {
if (snapshots.connectionState == ConnectionState.active &&
snapshots.hasData) {
return ListView(
physics: const BouncingScrollPhysics(),
children: snapshots.data!.map(
(DocumentSnapshot document) {
Map<String, dynamic> data =
document.data()! as Map<String, dynamic>;
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(40),
color: const Color(0xff303434),
),
child: Row(
children: [
//CODE OF PLACEHOLDER IMAGE
Padding(
padding: const EdgeInsets.all(15.0),
child: ClipRRect(
borderRadius: BorderRadius.circular(40.0),
child: Image.network(
'https://picsum.photos/250?image=9',
fit: BoxFit.fill,
width: 150,
height: 150,
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: 140,
),
child: AutoSizeText(
data['foodCategory'].toString(),
maxLines: 1,
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 25,
fontWeight: FontWeight.w900,
),
),
),
const SizedBox(
height: 5,
),
ConstrainedBox(
constraints: const BoxConstraints(
maxHeight: 70.0,
maxWidth: 140,
),
child: AutoSizeText(
data['allergens'],
minFontSize: 10,
maxFontSize: 25,
style: GoogleFonts.poppins(
color: Colors.white,
fontWeight: FontWeight.w600,
),
),
),
const SizedBox(
height: 5,
),
AutoSizeText(
data['ageNum'].toString() +
' ' +
data['ageUnit'].toString(),
maxLines: 1,
style: GoogleFonts.poppins(
color: Colors.white,
fontSize: 15,
fontWeight: FontWeight.w400,
),
),
],
),
],
),
);
},
).toList(),
);
} else {
return const Center(child: CircularProgressIndicator());
}
},
),
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的图像URL是错误的。
将图像上传到firestore
示例下面的firestore示例:
规则
如果您不想对firestore进行身份验证,则应在firestore上查看您的
Your image url are wrong.
You should get image url after upload image to Firestore
Example below code:
And iamgePath is downloadUrl
Check your rule on Firestore if you don't want to authenticate Firestore