Search code examples
flutterdartflutter-widget

How can I perform filtering/searching school by writing the email address in flutter?


I would like to implement filtering/searching the university by typing its name like this picture below using flutter as a beginner. I would like it to auto-filter then display the school name even when I don't type the whole email address. It is quite hard to find good examples for what I want to do. Does anyone know of good examples that implemented something like I want?

Thank you

enter image description here

enter image description here


Solution

  • This a simple search in list items based on the text written. Below is the solution i use to search items in a list and update the list accordingly.

    Assume you have a list of Emails. List emails = []; A list which consist of emails from server/source

    List emailsToShow = []; List of emails you update with every letter typed.

    searchEmail(String typedLetters) {
    if (typedLetters.length > 0) {
      emailsToShow.clear();
    
      emailsToShow.addAll(emails
          .where((element) =>
              element.toLowerCase().contains(typedLetters.toLowerCase()))
          .toList());
         setState((){});
    
          }
      }
    

    Now call this function from onChanged method of TextField and show emailsToShow list in ListView/ListView.builder and you are good to go.