removeCountryCode method

String removeCountryCode(
  1. String? countryCode,
  2. Country selectedCountry,
  3. List<Country> countryList
)

Implementation

String removeCountryCode(
    String? countryCode, Country selectedCountry, List<Country> countryList) {
  String number = this;
  debugPrint("number: $number");
  if (countryCode == null && number.startsWith('+')) {
    number = number.substring(1);
    // parse initial value
    selectedCountry = countries.firstWhere(
        (country) => number.startsWith(country.fullCountryCode),
        orElse: () => countryList.first);
    debugPrint("selectedCountry: $selectedCountry");
    // remove country code from the initial number value
    number = number.replaceFirst(
        RegExp("^${selectedCountry.fullCountryCode}"), "");
    return number;
  } else {
    selectedCountry = countryList.firstWhere(
        (item) => item.code == (countryCode ?? 'IN'),
        orElse: () => countryList.first);

    // remove country code from the initial number value
    if (number.startsWith('+')) {
      number = number.replaceFirst(
          RegExp("^\\+${selectedCountry.fullCountryCode}"), "");
    } else {
      number = number.replaceFirst(
          RegExp("^${selectedCountry.fullCountryCode}"), "");
    }
    return number;
  }
}