parseNumber method

TheNumber parseNumber({
  1. String? internationalNumber,
  2. String? dialCode,
  3. String? iso2Code,
  4. String? iso3Code,
  5. String? currency,
  6. String? name,
  7. String? englishName,
})

Returns a TheNumber,

Which will further contain all the vitals about the parsed country,

If you use the internationalNumber like +971565656565 of which +971 is the dial-code of the country, the returned TheNumber will be a complete object with all the vitals about the country,

final _numberString = "+971565656565";`
final theNumber = TheCountryNumber().parse(internationalNumber:_numberString);`
print(theNumber.dialCode);//prints +971
print(theNumber.number);//prints 565656565

If you use anything other than internationalNumber to parse for example dialCode iso2Code iso3Code currency name englishName the returned TheNumber will be complete without the TheNumber.number & TheNumber.internationalNumber component

final theNumber = TheCountryNumber().parseNumber(iso2Code: "IN");
print(theNumber.dialCode);//prints the international dial-code for this country "+91"
print(theNumber.number); //prints empty
//access country details
final country = theNumber.country;
print(country.englishName);//prints India
print(country.localName);//prints भारत (hope spelling is correct)
print(country.currency);//prints INR

If the library is unable to parse (which effectively means no data exists for the given parsable data) a NotANumber value will be returned, call TheNumber.isNotANumber to check is its a valid TheNumber

Implementation

TheNumber parseNumber({
  String? internationalNumber,
  String? dialCode,
  String? iso2Code,
  String? iso3Code,
  String? currency,
  String? name,
  String? englishName,
}) {
  if (_countries.isEmpty) throw Exception("library is not initialized");
  if (!_isNullOrEmpty(internationalNumber)) {
    final tmp = _countries.firstWhere(
      (element) {
        if (_isNullOrEmpty(element.dialCode)) {
          return false;
        } else {
          return internationalNumber?.startsWith(element.dialCode) ?? false;
        }
      },
      orElse: () {
        return _TheCountry();
      },
    );
    return _getNumberForCountry(tmp,
        internationalNumber: internationalNumber);
  }

  if (!_isNullOrEmpty(dialCode)) {
    final tmp = _countries.firstWhere(
      (element) {
        if (_isNullOrEmpty(element.dialCode)) {
          return false;
        } else {
          return element.dialCode == dialCode;
        }
      },
      orElse: () {
        return _TheCountry();
      },
    );
    return _getNumberForCountry(tmp);
  }

  if (!_isNullOrEmpty(iso2Code)) {
    final tmp = _countries.firstWhere((element) {
      if (_isNullOrEmpty(element.iso2Code)) {
        return false;
      } else {
        return element.iso2Code == iso2Code;
      }
    }, orElse: () {
      return _TheCountry();
    });
    return _getNumberForCountry(tmp);
  }
  if (!_isNullOrEmpty(iso3Code)) {
    final tmp = _countries.firstWhere((element) {
      if (_isNullOrEmpty(element.iso3Code)) {
        return false;
      } else {
        return element.iso3Code == iso3Code;
      }
    }, orElse: () {
      return _TheCountry();
    });
    return _getNumberForCountry(tmp);
  }
  if (!_isNullOrEmpty(currency)) {
    final tmp = _countries.firstWhere((element) {
      if (_isNullOrEmpty(element.currency)) {
        return false;
      } else {
        return element.currency == currency;
      }
    }, orElse: () {
      return _TheCountry();
    });
    return _getNumberForCountry(tmp);
  }

  if (!_isNullOrEmpty(name)) {
    final tmp = _countries.firstWhere(
      (element) {
        if (_isNullOrEmpty(element.name)) {
          return false;
        } else {
          return element.name == name;
        }
      },
      orElse: () {
        return _TheCountry();
      },
    );
    return _getNumberForCountry(tmp);
  }

  if (!_isNullOrEmpty(englishName)) {
    final tmp = _countries.firstWhere(
      (element) {
        if (_isNullOrEmpty(element.englishName)) {
          return false;
        } else {
          return element.englishName == englishName;
        }
      },
      orElse: () {
        return _TheCountry();
      },
    );
    return _getNumberForCountry(tmp);
  }
  return NotANumber();
}