Pranav Bahl
Pranav Bahl

Reputation: 21

How can I filter a list in Flutter based on the words it contains?

I have used .where on the List to filter like this:-

List<AppUsageInfo> get eyes => (_appInfo.where((element)=>element.packageName=='apple')).toList();

This works but I want to filter with more than just one word. When I try the following code:

List<AppUsageInfo> get eyes => (_appInfo.where((element)=>element.packageName=='apple'||'google')).toList();

I get the error 'The operands of the operator '||' must be assignable to 'bool.'

I tried using brackets around 'apple and 'google' to see if it changes anything but that didn't seem to work. Any help would me much appreciated!

Upvotes: 0

Views: 651

Answers (2)

BambinoUA
BambinoUA

Reputation: 7100

Use this

List<AppUsageInfo> get eyes => 
_appInfo.where((element)=>['apple', 'google'].contains(element.packageName)).toList();

Upvotes: 1

user7720975
user7720975

Reputation:

The operator || accepts only booleans for both of the operands of the operator.

_appInfo.where((element) => element.packageName == 'apple' || element.packageName == 'google').toList()

Upvotes: 0

Related Questions