【问题标题】:Flutter Dismissible not removing from treeFlutter Dismissible 未从树中移除
【发布时间】:2018-07-07 12:24:09
【问题描述】:

我正在使用 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)));
})),

【问题讨论】:

    标签: android listview flutter


    【解决方案1】:

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

    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)),
            );
    

    【讨论】:

    【解决方案2】:

    是的,因为只有密钥。

    key: new Key("newlist") - 错误

    应该是:

    key: Key(newlist[index])
    

    【讨论】:

      【解决方案3】:

      我解决了。本质上,我对每个 Dismissable 都使用相同的 Key。这让 Flutter 认为我解散的对象还在。希望这对某人有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-29
        • 2019-09-10
        • 2020-02-11
        • 2018-05-23
        • 2020-04-18
        相关资源
        最近更新 更多