(颤音)如何在Firestore中存储的路径中检索firebase存储图像

发布于 2025-01-28 20:23:48 字数 5008 浏览 4 评论 0原文

在我的代码中,我有一个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.
enter image description here

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.
enter image description here

The image path is a subItem in the test folder in Firebase Storage:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2025-02-04 20:23:48

您的图像URL是错误的。
将图像上传到firestore

示例下面的firestore示例:

var file = _imageFile;
    FirebaseStorage firebaseStorage = FirebaseStorage.instance;
    Reference ref = firebaseStorage.ref(
        'uploads-images/${user.userId}/images/${DateTime.now().microsecondsSinceEpoch}');
    TaskSnapshot uploadedFile = await ref.putFile(file);
    if (uploadedFile.state == TaskState.success) {
      downloadUrl = await ref.getDownloadURL();
    }

规则

如果您不想对firestore进行身份验证,则应在firestore上查看您的

Your image url are wrong.
You should get image url after upload image to Firestore

Example below code:

var file = _imageFile;
    FirebaseStorage firebaseStorage = FirebaseStorage.instance;
    Reference ref = firebaseStorage.ref(
        'uploads-images/${user.userId}/images/${DateTime.now().microsecondsSinceEpoch}');
    TaskSnapshot uploadedFile = await ref.putFile(file);
    if (uploadedFile.state == TaskState.success) {
      downloadUrl = await ref.getDownloadURL();
    }

And iamgePath is downloadUrl

Check your rule on Firestore if you don't want to authenticate Firestore

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文