0

我在颤动中使用AnimatedList为列表中的项目设置动画。将项目添加到列表时,动画正常工作。但是当一个项目被删除时,要么出现错误(见下文),要么正在动画错误的项目:https ://res.cloudinary.com/drcxef0qi/video/upload/v1645354884/5f5d2ff9-7e82-424a-96b5-ecf097a89316_bpojds .mp4

仅当删除的项目是列表中的最后一项时才会出现此错误: RangeError (index): Invalid value: Valid value range is empty: 0

这是我正在使用的代码:

void removeItem(String id) {
 int index = listofItems.indexWhere((e) => e['_id'] == id);

 listofItems.removeAt(index);
 _key.currentState!.removeItem(
    index, (_, animation) => listanim(animation, listofItems[index]),
    duration: const Duration(milliseconds: 500));
 }

动画列表:

AnimatedList(
  shrinkWrap: true,
  itemBuilder: (_, int index, Animation<double> animation) => listanim(animation, listofItems[index]),
  initialItemCount: listofItems.length,
  key: _key,
)

谢谢!

4

1 回答 1

0

发生这种情况是因为当您从列表中删除所有项目时,animatedList 将返回错误,因为没有项目

要解决这个问题,只需添加它以检查项目是否为空

          listofItems.isNotEmpty?
      AnimatedList(itemBuilder: itemBuilder,initialItemCount: index,)
      : const CircularProgressIndicator(),
于 2022-02-18T06:34:52.733 回答