I want search request on the List<Food>
that I got. I have used a query method like this:
_foodList.where((food) => food.name == userInputValue).toList();
however, the search asked me to search with complete text and the right capitalization of the text.
how if I want to process a compilation of "dish"
, then all the names of foods that have the word "dish"
will display in List
?
The contains() method is used to check if an element occurs in a list. This method takes one parameter and returns a boolean value indicating whether or not that item is found in the list.
The method indexWhere() returns the list's first index that matches the given condition. It searches the list from the beginning of the index to the end. The index of the item is returned the first time an item is encountered, ensuring that the condition is true. Otherwise, it returns -1.
Lower-case or upper-case all strings before comparison and use contains()
instead of ==
:
_foodList.where((food) => food.name.toLowerCase().contains(userInputValue.toLowerCase()).toList();
If values can be null
you need to add additional checks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With