Flutter Dismissible 不从树中移除

Roh*_*n D 4 android listview flutter

我正在使用 Flutter 并且正在努力从树中删除一个 Dismissible 对象。下面是我的代码。我创建了一个存储在列表“newlist”中的自定义类。我似乎从 List 和 setState() 中删除了 Dismissible 对象,但它似乎不起作用。任何帮助是极大的赞赏。

   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)));
})),
Run Code Online (Sandbox Code Playgroud)

Rub*_*yan 5

我已经使用项目名称 + 列表长度作为键解决了它。因为可能有一些项目具有相同的价值

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)),
        );
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用 UniqueKey() 来实现它。https://api.flutter.dev/flutter/widgets/UniqueKey-class.html (2认同)