Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)));
})),
like image 740
Rohan D Avatar asked Jan 28 '23 18:01

Rohan D


1 Answers

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)),
        );
like image 187
Ruben Martirosyan Avatar answered Jan 31 '23 19:01

Ruben Martirosyan