getAddress static method

Future<Map<String, String?>> getAddress({
  1. required String regionNo,
  2. required String provinceCode,
  3. required String municipalityCode,
  4. required String barangayCode,
})

Returns a complete PCIC address for the given code set.

Implementation

static Future<Map<String, String?>> getAddress({
  required String regionNo,
  required String provinceCode,
  required String municipalityCode,
  required String barangayCode,
}) async {
  final normalizedRegionNo = _normalizeValue(regionNo);
  final normalizedProvinceCode = _normalizeValue(provinceCode);
  final normalizedMunicipalityCode = _normalizeValue(municipalityCode);
  final normalizedBarangayCode = _normalizeValue(barangayCode);

  if (normalizedRegionNo == null ||
      normalizedProvinceCode == null ||
      normalizedMunicipalityCode == null ||
      normalizedBarangayCode == null) {
    return {
      'regionNo': null,
      'regionCode': null,
      'regionSubCode': null,
      'provinceCode': null,
      'provinceName': null,
      'municipalityCode': null,
      'municipalityName': null,
      'barangayCode': null,
      'barangayName': null,
    };
  }

  final barangays = await getBarangays(
    regionNo: normalizedRegionNo,
    provinceCode: normalizedProvinceCode,
    municipalityCode: normalizedMunicipalityCode,
  );
  final matchedBarangay = barangays.cast<Map<String, dynamic>?>().firstWhere(
    (barangay) =>
        _normalizeValue(barangay?['barangayCode']) == normalizedBarangayCode,
    orElse: () => null,
  );

  if (matchedBarangay == null) {
    return {
      'regionNo': null,
      'regionCode': null,
      'regionSubCode': null,
      'provinceCode': null,
      'provinceName': null,
      'municipalityCode': null,
      'municipalityName': null,
      'barangayCode': null,
      'barangayName': null,
    };
  }

  return {
    'regionNo': normalizedRegionNo,
    'regionCode': _normalizeValue(matchedBarangay['regionCode']),
    'regionSubCode': _normalizeValue(matchedBarangay['regionSubCode']),
    'provinceCode': normalizedProvinceCode,
    'provinceName': _normalizeValue(matchedBarangay['provinceName']),
    'municipalityCode': normalizedMunicipalityCode,
    'municipalityName': _normalizeValue(matchedBarangay['municipalityName']),
    'barangayCode': normalizedBarangayCode,
    'barangayName': _normalizeValue(matchedBarangay['barangayName']),
  };
}