4
\$\begingroup\$

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("Dr apj abdul Kalam"));
}

setSearchParam(String caseNumber) {
  List<String> caseSearchList = List();
  String temp = "";
  String temp2 = "";
  String temp3 = "";

  if (caseNumber.split(" ").length > 1) {
    String firstWord = caseNumber.split(" ")[0];
    String secondWord = caseNumber.split(" ")[1];
    String thirdWord = caseNumber.split(" ")[2];

    for (int i = 0; i < firstWord.length; i++) {
      temp = temp + firstWord[i];
      caseSearchList.add(temp);
    }
    for (int i = 0; i < secondWord.length; i++) {
      temp2 = temp2 + secondWord[i];
      caseSearchList.add(temp2);
    }
    for (int i = 0; i < thirdWord.length; i++) {
      temp3 = temp3 + thirdWord[i];
      caseSearchList.add(temp3);
    }
  } else {
    for (int i = 0; i < 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.

\$\endgroup\$

1 Answer 1

6
\$\begingroup\$

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("Dr apj abdul Kalam"));
  // [D, Dr, a, ap, apj, a, ab, abd, abdu, abdul, K, Ka, Kal, Kala, Kalam]

  print(searchParamWord('test'));
}

List<String> setSearchParam(String caseNumber) =>
    caseNumber.split(' ').expand(searchParamWord).toList();

Iterable<String> searchParamWord(String word) sync* {
  final sb = StringBuffer();

  for (var i = 0; i < 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 "(t, te, tes, test)" 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("Dr apj abdul Kalam"));
  // [D, Dr, A, Ap, Apj, A, Ab, Abd, Abdu, Abdul, K, Ka, Kal, Kala, Kalam]

  print(searchParamWord('test'));
}

List<String> setSearchParam(String caseNumber) =>
    caseNumber.split(' ').expand(searchParamWord).toList();

Iterable<String> searchParamWord(String word) sync* {
  final sb = StringBuffer();

  for (var i = 0; i < word.length; i++) {
    yield (i == 0)
        ? (sb..write(word[i].toUpperCase())).toString()
        : (sb..writeCharCode(word.codeUnitAt(i))).toString();
  }
}
\$\endgroup\$
4
  • \$\begingroup\$ bro can you help me to make the first letter of each word capitalized \$\endgroup\$ Jun 20, 2020 at 16:00
  • \$\begingroup\$ So we are talking about the same code but you want it do return: [D, Dr, A, Ap, Apj, A, Ab, Abd, Abdu, Abdul, K, Ka, Kal, Kala, Kalam] instead? \$\endgroup\$ Jun 20, 2020 at 16:33
  • \$\begingroup\$ yes bro.....thanks for replying..... \$\endgroup\$ Jun 20, 2020 at 16:34
  • \$\begingroup\$ Updated my answer with an example of how to do it. \$\endgroup\$ Jun 20, 2020 at 16:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.