【问题标题】:Receive the details of a collection from another collection Flutter Firebase从另一个集合 Flutter Firebase 接收集合的详细信息
【发布时间】:2022-06-30 11:19:23
【问题描述】:

我在 firebase 中创建了 2 个集合。 一个涉及更衣室的衣服,另一个涉及服装。 每件衣服都有一个带有衣服ID的数组。 我希望能够在我的视图中显示的每件衣服中接收我的衣服的详细信息。 目前我只在 FutureBuilder 中显示我的服装集合,如有必要,我设法在我的服装显示中显示 id,但我不明白如何与我的衣服对话以在我的视图中接收更多细节。

你有什么想法吗?

【问题讨论】:

  • 一些示例代码将有助于显示您的确切数据访问模式并提供更多上下文。

标签: flutter firebase dart google-cloud-firestore


【解决方案1】:

我不确定我是否完全理解您的问题。但我觉得你想从你的衣服集合中获取文档,这些文档与你的 Outfit 视图一起显示在 FutureBuilder() 中。取一套衣服并展示衣服。您需要将您的衣服 ID 存储在您的服装文档中的数组中。

然后在获取服装时,您从布料 ID 列表中获取布料。

如果您使用的是 FutureBuilder(完全不是最佳解决方案),您需要使用新请求嵌套您的 FutureBuilder,例如

FutureBuilder<DocumentSnapshot>(
  future: FirebaseFirestore.instance.collection("outfits").doc("outfit-id").get(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      if (snapshot.data?.exists == true) {
        final data = snapshot.data?.data() as Map<String, dynamic>;
        final clothIds = List<String>.from(data["cloth_ids"] ?? []);

        /// your outfit widgets here and cloths

        return Column(
          children: List.generate(
            clothIds.length,
            (index) {
              final clothId = clothIds[index];
              return FutureBuilder<DocumentSnapshot>(
                        future: FirebaseFirestore.instance.collection("cloths").doc(clothId).get(),
                        builder: (context, snapshot) {
                           if (snapshot.hasData) {
                               if (snapshot.data?.exists == true) {
                                   // your cloths widget here
                    }
                  }
                  return SizedBox.shrink();
                },
              );
            },
          ),
        );
      }
    }
    return SizedBox.shrink();
  },
);

在 youtube 上查看我的 firebase 教程here

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 2017-05-02
    相关资源
    最近更新 更多