searchStates method
Implementation
List<StateModel> searchStates(String query, String? countryId, {int limit = 50}) {
if (_states == null || query.isEmpty) return [];
final cacheKey = '${query.toLowerCase()}:$countryId:$limit';
if (_stateSearchCache.containsKey(cacheKey)) {
return _stateSearchCache[cacheKey]!;
}
final lowerQuery = query.toLowerCase();
final results = <StateModel>[];
final Set<String> addedIds = {};
// Use index for blazing fast search
_stateSearchIndex?.forEach((prefix, states) {
if ((prefix.startsWith(lowerQuery) || prefix == lowerQuery) && results.length < limit) {
for (final state in states) {
if (!addedIds.contains(state.id) && results.length < limit &&
(countryId == null || state.countryId == countryId)) {
results.add(state);
addedIds.add(state.id);
}
}
}
});
_manageCacheSize(_stateSearchCache, cacheKey, results);
return results;
}