getDistricts static method

List<District> getDistricts({
  1. required int provinceCode,
  2. String? query,
})

Gets the list of districts by province code.

Optionally filters by query.

Note: This method is only applicable for v1 (3-level structure). For v2 (2-level structure), this will return an empty list.

Implementation

static List<District> getDistricts(
    {required int provinceCode, String? query}) {
  if (_version == AdministrativeDivisionVersion.v2) {
    return [];
  }

  final province = _provinces.firstWhere(
    (p) => p.code == provinceCode,
    orElse: () =>
        throw ArgumentError('Province with code $provinceCode not found'),
  );

  return province.districts.where((district) {
    final normalizedQuery = query != null ? removeDiacritics(query) : null;
    final normalizedDistrictName = removeDiacritics(district.name);

    final matchesQuery = normalizedQuery == null ||
        normalizedDistrictName.contains(normalizedQuery);
    return matchesQuery;
  }).toList();
}