search method

Future<List<String>> search({
  1. required List<String> dataset,
  2. required String query,
  3. double? strictThreshold,
  4. double? typoThreshold,
  5. bool? kIsWeb,
})

Implementation

Future<List<String>> search({
  required List<String> dataset,
  required String query,
  double? strictThreshold,
  double? typoThreshold,
  bool? kIsWeb,
}) async {
  // Validate inputs
  if (dataset.isEmpty) {
    throw ArgumentError("Dataset cannot be empty.");
  }
  if (query.trim().isEmpty) {
    throw ArgumentError("Query cannot be empty or whitespace.");
  }
  if (strictThreshold != null &&
      (strictThreshold < 0 || strictThreshold > 1)) {
    throw ArgumentError("strictThreshold must be between 0 and 1.");
  }
  if (typoThreshold != null && (typoThreshold < 0 || typoThreshold > 1)) {
    throw ArgumentError("typoThreshold must be between 0 and 1.");
  }

  try {
    if (dataset.length > Constants.isolateThreshold && kIsWeb == false) {
      final result = await searchWithIsolate(
        dataset: dataset,
        query: query,
        strictThreshold: strictThreshold ?? Constants.defaultStrictThreshold,
        typoThreshold: typoThreshold ?? Constants.defaultStrictThreshold,
      );
      return result.map((e) => e['value'] as String).toList();
    } else {
      final result = searchLocally(
        dataset: dataset,
        query: query,
        strictThreshold: strictThreshold ?? Constants.defaultStrictThreshold,
        typoThreshold: typoThreshold ?? Constants.defaultStrictThreshold,
      );
      return result.map((e) => e['value'] as String).toList();
    }
  } catch (e) {
    rethrow;
  }
}