简体   繁体   中英

Flutter Dismissible not removing from tree

I am working with Flutter and am struggling to remove a Dismissible object from the tree. Below is my code. I have created a custom class that is stored in the list 'newlist.' I seemingly remove the Dismissible object from the List and setState(), but it does not seem to work. Any help is greatly appreciated.

   return new Dismissible(key: new Key("newlist"),
direction: DismissDirection.horizontal,
onDismissed: (DismissDirection direction) {
            setState(() {
              newlist.remove(newlist[index]);
              print(newlist.length);

            });
},
child: new ListTile(
leading: const
Icon(Icons.album),
title: new Text(newlist[index].amount),
subtitle: new Text(
newlist[index].name)));
})),

I have solved it using items name + lists length as a key. Because there could be some items with the same value

return Dismissible(

          key: Key(item.name + _paths.length.toString()),

          onDismissed: (direction) {
            setState(() {
              _paths.removeAt(index);
            });

          // Show a red background as the item is swiped away
          background: Container(color: Colors.red),
          child: Container(child: new Texts().tallText(item.name)),
        );

I solved it. Essentially, I was using the same Key for every Dismissable. This makes Flutter think that the object I dismissed is still there. Hope this helps someone.

Yes, It because of Key only.

key: new Key("newlist") - wrong

it should be:

key: Key(newlist[index])

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM