startsWith method

List<String> startsWith(
  1. Iterable<String> terms, [
  2. int limit = 10
])

Returns a subset of candidates that starts with the same characters as the String.

The String and terms are converted to lower-case and trimmed for the comparison.

Not case sensitive.

Implementation

List<String> startsWith(Iterable<String> terms, [int limit = 10]) {
  final term = trim().toLowerCase();
  if (term.isEmpty) {
    return [];
  }
  final startsWithTerms = terms
      .map((e) => e.trim().toLowerCase())
      .where((element) => element.startsWith(term))
      .toSet()
      .toList();
  startsWithTerms.sort(((a, b) => a.compareTo(b)));
  startsWithTerms.sort(((a, b) => a.length.compareTo(b.length)));
  return startsWithTerms.length > limit
      ? startsWithTerms.sublist(0, limit)
      : startsWithTerms;
}