【问题标题】:Flutter.io (Dart) - How to count items returned from Firebase query that meet certain conditionsFlutter.io (Dart) - 如何计算从 Firebase 查询返回的满足特定条件的项目
【发布时间】:2018-06-12 18:16:35
【问题描述】:

在屏幕中,Firebase 列表具有应用于

的逻辑
  1. 被动检测小部件的蓝色
  2. 隐藏蓝色小部件
  3. 显示非蓝色小部件

我想数一下蓝色小部件。

最好的方法是什么?

return new StreamBuilder<int>(
    stream: subscribeMyThings(thing.id),
    builder: (context, thingsSnapshot) {
        return new FirebaseAnimatedList(
          query: getThings(thing.id),
          itemBuilder: (context, snapshot, animation, index) {
            return new ReactiveWidget<int>(
              reactiveRef: getThingBlueness(snapshot.key),
              widgetBuilder: (checkBlueness) {
                // code to check blueness here

                // if Thing is blue, place empty container
                if (thingIsBlue) {
                  return new Container();
                  // *** how do I count these??? ***
                }

                // or, if Thing is NOT blue
                return new Thing(thing.id);
                );
              },
            );
        );
      }

【问题讨论】:

    标签: firebase dart flutter


    【解决方案1】:

    我认为您没有从正确的角度解决问题,而且标题具有误导性。

    您可能不打算计算屏幕上显示的小部件,而是计算符合某些条件的 Firebase 数据库条目子集。 这在 Flutter 中产生了很大的不同,因为计算小部件涉及到渲染树、布局和一般图形,而看起来你想要解决数据问题。

    如果我是对的,那么您可能会考虑在您的数据库中建立一个索引来跟踪蓝色项目的数量并在另一个流构建器中收听它。

    长话短说,您可能想要保留地图,在构建时更新每个元素,并在需要计数时迭代地图。

    // somewhere in your outer scope
    Map<int, bool> _bluenessMap = new Map<int, bool>();
    
      return new StreamBuilder<int>(
        stream: subscribeMyThings(thing.id),
        builder: (context, thingsSnapshot) {
            return new FirebaseAnimatedList(
              query: getThings(thing.id),
              itemBuilder: (context, snapshot, animation, index) {
                return new ReactiveWidget<int>(
                  reactiveRef: getThingBlueness(snapshot.key),
                  widgetBuilder: (checkBlueness) {
                    // code to check blueness here
    
    
                    // ----------------------------
                    // before return let us update the map
    
                    _bluenessMap[thing.id] = thingIsBlue
    
                    // ----------------------------
    
                    // if Thing is blue, place empty container
                    if (thingIsBlue) {
                      return new Container();
                    }
    
                    // or, if Thing is NOT blue
                    return new Thing(thing.id);
    
                    );
                  },
                );
            );
          }
    
      // call me when you want to count
      int fancyBlueCounter(){
          int result = 0;
          for(bool isBlue in _bluenessMap.values){
            if(isBlue) result += 1;
          }
          return result;
      }
    

    编辑

    由于该问题的作者确认这是一个“数据问题”,我想建议另一种(也许更好的)方法来解决它。

    聚合或索引(在我看来)是解决问题的正确方法。任何时候有人根据它的蓝色触摸一个事物,它会刷新数据库中有意创建的聚合(实际上也是一个简单的数据输入count)。您可以在任何功能编辑 事物 之后使用 事务 更新 蓝色计数,或使用 云功能 来执行此操作(参见 doc here)监听更改事件(创建、删除和编辑)并更新 count

    【讨论】:

    • 你说的很对,我想统计一个满足某些条件的数据子集。这看起来应该可以很好地工作,但我无法让它工作。我会继续努力,如果我能正常运行,我会标记为答案。
    • 编辑标题和问题以更好地反映这一点。
    • @Deborah 正如我所提到的,仅使用您提供的代码很难提供一个最小的工作示例。无论如何,如果可以的话,我很乐意提供帮助,请随时提供有关结果的更多细节。
    • @Deborah 请检查更新后的答案中的“数据”解决方案。
    • 我完全同意@fabio-veronese。我在使用 Firestore 的颤振应用程序中遇到了您的确切问题,并最终创建了与我的主集合相邻的文档 ID 集合,即我的主集合中的每个文档的 ID 都放在一个单独的 ID 集合中。我只需要在应用程序中添加一些开销来管理/使用与主要 Firestore 流并行的 id 集合。工作得很好。我认为 Firebase 实时数据库可能会更好地工作,但我认为就你和我正在尝试做的事情而言,它会是相似的。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 1970-01-01
    • 2020-09-28
    • 2022-06-22
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    相关资源
    最近更新 更多