tryParse static method

IsoCountry? tryParse(
  1. String source
)

Parses source as Alpha-2, alpha-3 or numeric country code. Same as parse but returns null in case of invalid country code

Implementation

static IsoCountry? tryParse(String source) {
  final numeric = int.tryParse(source)?.toString().padLeft(3, "0");
  if (null != numeric)
    return IsoCountries.values.firstOrNullWhere((e) => e.numeric == numeric);

  final upperCaseSource = source.toUpperCase();
  switch (upperCaseSource) {
    case String(length: 2):
      return IsoCountries.values
          .firstOrNullWhere((e) => e.alpha2 == upperCaseSource);

    case String(length: 3):
      return IsoCountries.values
          .firstOrNullWhere((e) => e.alpha3 == upperCaseSource);

    default:
      return null;
  }
}