【问题标题】:Flutter Streambuilder is duplicating itemsFlutter Streambuilder 正在复制项目
【发布时间】:2021-04-09 01:10:12
【问题描述】:

我有一个 StreamBuilder 连接到一个列表,并且该列表从 Firebase 获取其数据,但是我的数据库中的每个事件都会重复我的 Streambuilder 中的项目。

这是我的 StreamBuilder 代码:

StreamBuilder(
                  stream: masterListStart().asStream(),
                  builder: (context, snapshot) {
                    //return coursesList.length == 0
                    return finishedLoadingList
                        ? ListView.builder(
                            scrollDirection: Axis.horizontal,
                            shrinkWrap: true,
                            itemCount: storysList.length,
                            itemBuilder: (context, index) {
                              //
                              StoryItems data = storysList[index];
                              //
                              return StoryItems(
                                data: data,
                              );
                            },
                          )
                        : CircularProgressIndicator();
                  },
                ),

如何防止 StreamBuilder 这样做?

【问题讨论】:

  • finishedLoadingList 变量在哪里?
  • 检索完成后在我的setState中。数据会显示,但会在数据库更改时自行复制

标签: flutter google-cloud-firestore stream-builder


【解决方案1】:

除了使用finishedLoadingList,您可以简单地检查您的快照连接状态,如下所示

StreamBuilder(
  stream: masterListStart().asStream(),
  builder: (context, snapshot) {
    //return coursesList.length == 0
    return (ConnectionState.done == snapshot.connectionState) ? ListView.builder(
      scrollDirection: Axis.horizontal,
      shrinkWrap: true,
      itemCount: storysList.length,
      itemBuilder: (context, index) {
        //
        StoryItems data = storysList[index];
        //
        return StoryItems(
          data: data,
        );
      },
    ) : CircularProgressIndicator();
  },
),

【讨论】:

  • 先生,您是最棒的,非常感谢您,它工作得很好。最好的问候
  • 很高兴它有帮助!快乐编码
  • 谢谢你,先生,你来
  • 您好,我再次启动了代码,但问题再次发生我尝试重建所有内容并卸载应用程序检查代码等,但看起来它又发生了。还有其他解决方案吗?
  • 谢谢。顺便说一句,很抱歉让你再次帮助我,然后我自己解决了,这并不是要浪费你的时间。
猜你喜欢
  • 2020-12-16
  • 2020-04-29
  • 2021-10-12
  • 1970-01-01
  • 2018-12-28
  • 2021-09-30
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
相关资源
最近更新 更多