Flutter - How to query search item in List

Viewed 34104

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?

1 Answers

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.

Related