flutterindexoutofboundsexceptionreorderable-list

Flutter: How do I update ReorderableListView's list count as I remove an item?


I want to remove an item and add it to another list. The index found for each item is also always 0. When the first one is successfully removed then the below error is thrown:

Assertion failed: file:///C:/Users/User/flutter/packages/flutter/lib/src/widgets/reorderable_list.dart:652:12 0 <= index && index < widget.itemCount is not true

child: IconButton(
                            icon: const Icon(Icons.delete_sweep),
                            onPressed: () {
                              int selectedIndex = _items.indexOf(item);
                              _items.removeAt(selectedIndex);
                              _removeditems.add(item);
                            )


Solution

  • As I mentioned in comment, you missed to have setState

    Code should be like..

    child: IconButton(
      icon: const Icon(Icons.delete_sweep),
      onPressed: () {
        setState((){
          int selectedIndex = _items.indexOf(item);
          _items.removeAt(selectedIndex);
          _removeditems.add(item);
        });
      )