【问题标题】:AnimatedList out of range when StreamBuilder refreshesStreamBuilder 刷新时 AnimatedList 超出范围
【发布时间】:2022-01-14 22:14:36
【问题描述】:
StreamBuilder(
          stream: users.where("invites", arrayContains: uid).snapshots(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasData) {
              _items = snapshot.data!.docs;
              return AnimatedList(
                key: listKey,
                initialItemCount: _items.length,
                itemBuilder: (context, index, animation) {
                  return _buildItem(index, _items[index], animation);
                },
              );
            } else {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [
                  Center(
                    child: SizedBox(
                        height: 50,
                        width: 50,
                        child: CupertinoActivityIndicator()),
                  ),
                ],
              );
            }
          },
        )

我从 FirebaseFirestore 获得了这个带有流的 StreamBuilder。例如。我在 Firebase 中有 3 个符合我的条件的条目(保存在 _items 中)。到目前为止,一切都很好。但是,如果我删除其中一个,流会刷新,但列表会抛出错误: RangeError (index): Invalid value: Not inclusive range 0..1: 2 其他方式解决同样的问题:只显示了两个,第三个没有显示。 有谁知道为什么或如何解决这个问题。 谢谢:)

【问题讨论】:

    标签: flutter flutter-animatedlist


    【解决方案1】:

    这不适用于动画列表,因为您的索引正在减少。您需要构建一个新的动画列表,否则我建议您使用普通的列表视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-16
      • 1970-01-01
      • 2015-02-13
      • 2021-08-27
      • 2023-03-24
      • 2017-04-03
      • 2012-05-04
      • 2020-01-16
      相关资源
      最近更新 更多