【问题标题】:How do I get rid of the onDismissible handler error?如何摆脱 onDismissible 处理程序错误?
【发布时间】:2021-01-06 19:03:26
【问题描述】:

我正在尝试使用 Dismissible() 从交易列表和应用程序屏幕中删除其中一项交易。但我得到了错误

已关闭的 Dismissible 小部件仍然是树的一部分。

确保实现 onDismissed 处理程序,并在该处理程序触发后立即从应用程序中删除 Dismissible 小部件。

我检查了similar question,有人建议将key: Key() 从索引更改为UniqueKey(),因为我的交易有一个 id,但我仍然收到上述错误。

这是我想要做的:

class TransactionList extends StatefulWidget {
  final List<Transaction> transactions;
  final Function deleteTx;

  TransactionList(this.transactions, this.deleteTx);

  @override
  _TransactionListState createState() => _TransactionListState();
}

class _TransactionListState extends State<TransactionList> {
  @override
  Widget build(BuildContext context) {
    return Container(
      height: 350,
      child: widget.transactions.isEmpty
          ? Column(...
            )
          : ListView.builder(
              itemBuilder: (ctx, index) {
                return Dismissible(
                  key: Key(widget.transactions[index].getId()),
                  onDismissed: (direction) {
                    setState(() {
                      widget.deleteTx;
                    });
                    Scaffold.of(context).showSnackBar(
                      SnackBar(
                          content: Text("${widget.transactions[index].getTitle()} dismissed")),
                    );
                  },
                  background: Container(
                    color: Colors.red,
                  ),
                  child: Card(
                    elevation: 5,
                    margin: EdgeInsets.symmetric(
                      vertical: 8,
                      horizontal: 5,
                    ),
                    child: ListTile(

    //Formatting the amount entered of the transaction 

                      leading: CircleAvatar(
                        ... ,
                          ),
                        ),
                      ),

    //formatting the name/title of the transaction

                      title: Text(
                        ... ,
                      ),

//formatting the date of the transaction
    
                      subtitle: Text(
                        ... ,
                      ),

// adding a delete icon in the transaction card

                      trailing: IconButton(
                        icon: Icon(Icons.delete),
                        color: Theme.of(context).errorColor,
                        onPressed: () =>
                            widget.deleteTx(widget.transactions[index].getId()),
                      ),
                    ),
                  ),
                );
              },
              itemCount: widget.transactions.length,
            ),
    );
  }
}

deletetx 只是我的 main.dart 文件中的一个函数,它正在删除事务并更改状态。

void _deleteTransaction(String id) {
    setState(() {
      _userTrx.removeWhere((element) {
        return element.getId() == id;
      });
    });
  }

【问题讨论】:

    标签: android ios flutter dart


    【解决方案1】:

    我把上面代码sn-p中的onDismissed()改成了:

    onDismissed: (direction) => widget.deleteTx( widget.transactions[index].getId() ),
    

    它对我有用。

    【讨论】:

    • 您应该将其标记为已回答,以便其他人更容易找到 findnit。
    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多