Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - How to query search item in List

Tags:

list

flutter

dart

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?

like image 441
Muhammad Imanudin Avatar asked Jan 17 '19 09:01

Muhammad Imanudin


People also ask

How do you check if an element is in a list Flutter?

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.

How do you use Dart indexWhere?

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.


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.

like image 136
Günter Zöchbauer Avatar answered Oct 02 '22 17:10

Günter Zöchbauer