I have search field that filter my list and it works fine, but I want to filter list by multiple elements
My list:
List<Product> _p = [
Product(
title : 'P1',
info : 'just Football',
seller : 'me',
),
Product(
title : 'P2',
info : 'just Football',
seller : 'other',
),
]
filter method
List<Product> filterByText(text){
return _p.where((element) =>
element.title.contains(text) ||
element.info.contains(text) ||
element.seller.contains(text)
).toList();
}
I want, when I type Football other
in TextFormField
, to get this item:
Product(
title : 'P2',
info : 'just Football',
seller : 'other',
),
Instead, it returns an empty list. It worked when I type just other
. How can I filter with multiple elements by single string?
2
Answers
There are many ways to do this. I’ll go with one approach:
You can implement
Product
‘stoString()
method like so:Now, implement a fuzzy search, for
filterByText
. To do this, you can use an existing package like https://pub.dev/packages/fuzzywuzzy.You don’t have to necessarily implement
toString()
you can also just write'${x.title} ${x.info} ${x.seller}'
in the getter field ofextractTop
directly.You can break down the initial search query into words and then search all the words. Combine the results at the end.
Please ensure you override the
==
for classProduct
.This might help you further: multiple word search in flutter