mreFilterSuggestions function Fields

List<String> mreFilterSuggestions(
  1. List<String> all,
  2. String query, {
  3. int limit = 20,
})

Returns the suggestions in all that match query, at most limit.

A suggestion matches when it contains query, ignoring case, and is not equal to it. An empty query matches everything. The search stops as soon as limit matches are found.

Implementation

List<String> mreFilterSuggestions(
  List<String> all,
  String query, {
  int limit = 20,
}) {
  final needle = query.trim().toLowerCase();
  final matches = <String>[];

  for (final suggestion in all) {
    if (matches.length >= limit) {
      break;
    }
    final lower = suggestion.toLowerCase();
    if (needle.isEmpty || (lower.contains(needle) && lower != needle)) {
      matches.add(suggestion);
    }
  }
  return matches;
}