javaland235
javaland235

Reputation: 803

Dart: issue with where() function for filtering list

I am having an issue to filter a list based on a predicate. I have a list of string called _expenses where I am trying to filter out the expenses which belong to certain "Categories". But IDE gives me error in the boolean predicate saying "Avoid using unnecessary statement". Also during compilation it gives another error saying "flutter: Failed assertion: boolean expression must not be null". What am I doing wrong?

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

Upvotes: 1

Views: 1063

Answers (1)

Alexandre Ardhuin
Alexandre Ardhuin

Reputation: 76183

The lambda used as where argument don't return anything (a return is missing).

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

Upvotes: 2

Related Questions