Search filter for words in dart I need to add search filters for firebase So that I can get search result easily and precisely
I was altering the code from hereThis Medium Post
I would like to optimize this code.
void main() {
  print(setSearchParam(&quot;Dr apj abdul Kalam&quot;));
}

setSearchParam(String caseNumber) {
  List&lt;String&gt; caseSearchList = List();
  String temp = &quot;&quot;;
  String temp2 = &quot;&quot;;
  String temp3 = &quot;&quot;;

  if (caseNumber.split(&quot; &quot;).length &gt; 1) {
    String firstWord = caseNumber.split(&quot; &quot;)[0];
    String secondWord = caseNumber.split(&quot; &quot;)[1];
    String thirdWord = caseNumber.split(&quot; &quot;)[2];

    for (int i = 0; i &lt; firstWord.length; i++) {
      temp = temp + firstWord[i];
      caseSearchList.add(temp);
    }
    for (int i = 0; i &lt; secondWord.length; i++) {
      temp2 = temp2 + secondWord[i];
      caseSearchList.add(temp2);
    }
    for (int i = 0; i &lt; thirdWord.length; i++) {
      temp3 = temp3 + thirdWord[i];
      caseSearchList.add(temp3);
    }
  } else {
    for (int i = 0; i &lt; caseNumber.length; i++) {
      temp = temp + caseNumber[i];
      caseSearchList.add(temp);
    }
  }
  return caseSearchList;
}

OutPuts are like List of each word letters = [D, Dr, a, ap, apj, a, ab, abd, abdu, abdul]
I am pushing these lists to firebase so that I can get search results easily...
if there is an alternative for searching in firebase, then pls let me know...
if anyone still didn't understand my question, pls look at the link I provide above.
I have tried make a more optimized version here which also will work for string of any sizes and any number of words:
void main() {
  print(setSearchParam(&quot;Dr apj abdul Kalam&quot;));
  // [D, Dr, a, ap, apj, a, ab, abd, abdu, abdul, K, Ka, Kal, Kala, Kalam]

  print(searchParamWord('test'));
}

List&lt;String&gt; setSearchParam(String caseNumber) =&gt;
    caseNumber.split(' ').expand(searchParamWord).toList();

Iterable&lt;String&gt; searchParamWord(String word) sync* {
  final sb = StringBuffer();

  for (var i = 0; i &lt; word.length; i++) {
    yield (sb..writeCharCode(word.codeUnitAt(i))).toString();
  }
}

I have split the logic of your code into two parts.
searchParamWord takes one word and split it into its components like &quot;(t, te, tes, test)&quot; and returns a lazy evaluated iterator. This iterator can then later be used by expand in setSearchParam where we split the long String into each separate word.
By request, example how to upper case first letter in each word
void main() {
  print(setSearchParam(&quot;Dr apj abdul Kalam&quot;));
  // [D, Dr, A, Ap, Apj, A, Ab, Abd, Abdu, Abdul, K, Ka, Kal, Kala, Kalam]

  print(searchParamWord('test'));
}

List&lt;String&gt; setSearchParam(String caseNumber) =&gt;
    caseNumber.split(' ').expand(searchParamWord).toList();

Iterable&lt;String&gt; searchParamWord(String word) sync* {
  final sb = StringBuffer();

  for (var i = 0; i &lt; word.length; i++) {
    yield (i == 0)
        ? (sb..write(word[i].toUpperCase())).toString()
        : (sb..writeCharCode(word.codeUnitAt(i))).toString();
  }
}