首页 > 解决方案 > Flutter AnimatedList 更新列表长度失败

问题描述

我正在实现一个 Flutter 应用程序并使用AnimatedList. 在此列表中查看的数据是从控制器更新的,并且一旦完成任何更新,就会通知该列表。

当从控制器更新项目时,即添加或删除项目时,我调用setState以更新视图。它被正确调用,构建方法被调用,动画列表读取新的列表长度,但它仍然显示旧的,因此它out of index在列表末尾显示错误。

这就是我创建的方式AnimatedList

AnimatedList(
        controller: controller,
        key: GlobalKey(),
        initialItemCount: list.length,
        itemBuilder: (context, index, animation) {
          //the item builder method
        },
      )

Widget我调用的控制器的侦听器中:

setState(() {
  print('Update lists here');
});

那有什么问题呢?如何AnimatedList使用控制器中的新列表更新并反映其新长度?

请注意,当我从使用列表中删除项目时,不会发生此错误Widget

listKey.currentState.removeItem(index, animationBuilder);
list.removeAt(index);

标签: listflutterdartflutter-animatedlist

解决方案


推荐阅读