I was usingAnimatedList in flutter for animating the items in the list. The animation is working properly when an item is added to the list. But when an item is removed, either an error comes (see below) or the wrong item is being animated: https://res.cloudinary.com/drcxef0qi/video/upload/v1645354884/5f5d2ff9-7e82-424a-96b5-ecf097a89316_bpojds.mp4
This error only comes when the item deleted is the last one in the list: RangeError (index): Invalid value: Valid value range is empty: 0
Here's the code I'm using:
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:
AnimatedList(
shrinkWrap: true,
itemBuilder: (_, int index, Animation<double> animation) => listanim(animation, listofItems[index]),
initialItemCount: listofItems.length,
key: _key,
)
Thank you!
this happen beacuse when you remove all items from the list the animatedList will return error because there is no items
to fix that just add this to check if the items is empty or not
listofItems.isNotEmpty?
AnimatedList(itemBuilder: itemBuilder,initialItemCount: index,)
: const CircularProgressIndicator(),
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.