Search code examples
flutterlistviewflutter-layoutlistviewitem

Flutter delete an item in ListView.builder onTap of a button


I've create a ListView.builder to allow the user to add multiple credit cards.
To delete of them he would just check the card, and press the button 'Delete a card'. Inside the class InputAddCarte I've created a widget called _getItemChecked where I select the checked card and once my button is pressed I use removeAt to delete it.
My problem is that it doesn't work, it keeps telling me:

The following RangeError was thrown building:
RangeError (index): Invalid value: Only valid value is 0: 1

While if you check in the consolde of my dartPad, when I print the index it's either 0 or 1 (hence why I put index-1 in the code). If someone could help me find what I am doing wrong would be super nice! Here is the code: https://dartpad.dev/b0aaaa2901aa3ac67426d9bdd885abb1


Solution

  • You forgot to decrease your variable 'value' by one when removing an item from the list!