我在颤动中使用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,
)
谢谢!