How to solve: Flutter Dismissible insists list item must be removed from tree


Asked by iBob101 on August 28, 2017 (source).

I am using a list of Dismissible items and want a swipe in one direction to delete the item but a swipe in the other direction to initiate an edit of the item. However, Flutter insists that a Dismissible item must be removed from the tree in the onDismissed callback. I've tried re-inserting the item but that doesn't work. Any ideas? Extract from the code creating the list items is below:

  return new Dismissible(
    key: new ObjectKey(item),
    direction: DismissDirection.horizontal,
    onDismissed: (DismissDirection direction) {
      setState(() {
      if (direction == DismissDirection.endToStart){
        //user swiped left to delete item
        _scaffoldKey.currentState.showSnackBar(new SnackBar(
          content: new Text('You deleted: ${item.title}'),
          action: new SnackBarAction(
            label: 'UNDO',
            onPressed: () { handleUndo(item); }
      if (direction == DismissDirection.startToEnd){
        //user swiped right to edit so undo the delete required by flutter


Question answered by Collin J (source).

The Dismissible will think your item was dismissed as long as the item key changes. Let's say your item class is MyItem. If you implement a constructor MyItem.from in your MyItem class that copies the fields over, e.g.:

class MyItem {
  MyItem({ @required this.title, @required this.color });
  MyItem.from(MyItem other) : title = other.title, color = other.color;
  final String title;
  final Color color;

Then you can replace handleUndo(item) with handleUndo(new MyItem.from(item)) so that your new ObjectKey(item) will be unique from the old ObjectKey that you used before (assuming you didn't implement operator == on MyItem).