【问题标题】:How can I query another collection inside a streambuilder that queries a different collection?如何在查询不同集合的流构建器中查询另一个集合?
【发布时间】:2020-11-16 06:09:31
【问题描述】:

我正在尝试从与 streambuilder 不同的 firestore 集合中查询文档字段。现在,使用流构建器,我可以查询我需要的一个文档,但是我不知道如何在同一个流构建器中查询另一个集合。我考虑过使用另一个类中的全局变量,我已经在其中查询了我想要的文档,但这似乎不起作用。

这是流生成器

Container(
              height: MediaQuery.of(context).size.height,
              margin: EdgeInsets.only(top: 0),
              padding: EdgeInsets.only(top: 0),
              child: StreamBuilder(
                  stream: Firestore.instance
                      .collection('Tournaments')
                      .document('Fortnite Tournaments')
                      .collection('Fortnite Tourneys')
                      .document(docID)
                      .snapshots(),
                  builder: (context, snapshot) {
                    if (!snapshot.hasData) {
                      return new Text("Loading");
                    }
                    var tourneyDetails = snapshot.data;
                    return ListView(
                      children: <Widget>[ 
                            ..........

我要查询的集合称为“用户”
为什么我需要查询不同集合中的字段是因为我正在编写一个需要两个字段的 if 语句

if (int.parse("Field_i_am_trying_to_get_from_another_collection") > int.parse(tourneyDetails[ 'tourneycost']))
                               print('You have the money');                                                   
                                       else {
                                      print('You're broke');
                                        }

当点击凸起的按钮时,该条件将被执行。本质上,我的问题是我目前不确定如何从其他集合中查询数据。

不确定我是否解释得很好,但如果您需要更多上下文或代码,请发表评论,谢谢。

【问题讨论】:

标签: flutter dart google-cloud-firestore


【解决方案1】:

您可以使用提供程序而不是创建全局变量。 请参阅提供程序包:https://pub.dev/packages/provider

如果当前用户是您想要的用户,那么您可以在应用加载时从 firestore 获取当前用户数据,并将用户提供给您想要使用的位置。

如果您不知道提供者是什么,请参阅这篇文章: Flutter StateManagement with Provider

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    相关资源
    最近更新 更多