getCountryCode static method
Get country code from various input formats
Implementation
static String? getCountryCode(String input) {
if (input.isEmpty) return null;
final cleanInput = input.toLowerCase().trim();
// Check if it's already a 2-letter ISO code
if (cleanInput.length == 2) {
final upperCode = cleanInput.toUpperCase();
if (countryMapping.containsKey(upperCode)) {
return upperCode;
}
}
// Check if it's a 3-letter ISO code
if (cleanInput.length == 3) {
final upperCode = cleanInput.toUpperCase();
// Convert 3-letter to 2-letter using reverse mapping
for (final entry in countryMapping.entries) {
if (entry.key == upperCode) {
return entry.key;
}
}
}
// Check if it's a country name (with or without dashes)
final nameVariations = [
cleanInput,
cleanInput.replaceAll(' ', '-'),
cleanInput.replaceAll('-', ' '),
cleanInput.replaceAll('_', '-'),
];
for (final variation in nameVariations) {
if (countryMappings.containsKey(variation)) {
return countryMappings[variation];
}
}
// Try partial matching for country names
for (final entry in countryMappings.entries) {
if (entry.key.contains(cleanInput) || cleanInput.contains(entry.key)) {
return entry.value;
}
}
return null;
}