flutterdarttypeahead

How to remove whitespace in typeahed


I'm working on a typeahead search in flutter and let's say the user searches for bengali biriyani and return some names based on those two typed in words.

I'm splitting the search query like below:

List<String> searchWordList = search.toLowerCase().split(' ');

So List searchWordList is suppose to have ['Bengali','Biriyani']. But once I typed in bengali and put in a space the list(I'm printing the list on every turn) becomes something like this:

['Bengali', ' '] 

It gets fixed as soon as I start typing something else but this should not happen.

Here are the visual presentation of the problem:

Types in the query

consoled output

I'm guessing split() is not the way to go in this cases. But if so what can I do to mitigate this issue?


Solution

  • You can use trim() to remove the extra whitespace:

    List<String> searchWordList = search.trim().toLowerCase().split(' ');