1

Using String.contains(substring) to generate a list out of another fails when there is a space in my input.

Here is my code :

List<String> finalSuggestions = suggestions.where((i) => i.contains(new RegExp(pattern,caseSensitive:false, unicode: true))).toList();

As suggestions is a list of strings, and pattern is the user input. everything works fine unless the users adds a space in the string he is typing . finalSuggesions becomes null. else .. as long as the pattern is only a single word it is all fine.

3
  • How about you erase all space before run 'suggestions.where'? Commented Sep 4, 2020 at 18:07
  • If none of your suggestions contains a space, then it makes sense that a space in the pattern would result in no matched suggestions. What exactly is your question here? Commented Sep 4, 2020 at 19:22
  • all of my suggestions have a space between 2 words .. but when the user finishes the 1st word then press space .. the result shows noting !! Commented Sep 4, 2020 at 21:03

1 Answer 1

1

you don't need to use RegExp, Because pattern a string and suggestions a list of String.

List<String> finalSuggestions = suggestions.where((i) => i.contains(pattern)).toList();

It shouldn't be affected by space in this case

Sign up to request clarification or add additional context in comments.

2 Comments

A space isn't a special character, and even if it was, special characters don't need to be escaped when they come from user input.
Now your code won't compile because pattern.toString is a method and needs to be called with (). Regardless, though, pattern is already a string, so calling toString on it is redundant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.