gpt4 book ai didi

filter - Dart:where()函数用于过滤列表的问题

转载 作者:行者123 更新时间:2023-12-03 03:26:19 24 4
gpt4 key购买 nike

我遇到了一个基于谓词过滤列表的问题。我有一个名为_expenses的字符串列表,我试图在其中过滤出属于某些“类别”的费用。但是IDE在 bool(boolean) 谓词中给我一个错误,提示“避免使用不必要的语句”。同样在编译过程中,它还会给出另一个错误,说明“ flutter :断言失败: bool(boolean) 表达式不能为空”。我究竟做错了什么?

 this._expenses.where((Expense e) {
e.category != 'Deposit' || e.category != 'Loan (Inward)/Debt' || e.category != 'Loan(Outward) Settlement';
}).toList().forEach((Expense e){
totalExpense += e.amount;
});

最佳答案

用作where参数的lambda不返回任何内容(缺少return)。

this._expenses.where((Expense e) {
return e.category != 'Deposit' || e.category != 'Loan (Inward)/Debt' || e.category != 'Loan(Outward) Settlement';
}).toList().forEach((Expense e){
totalExpense += e.amount;
});

关于filter - Dart:where()函数用于过滤列表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54528414/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com