byPhone static method
The country whose dial code starts phone, a number in international
format such as +447700900123.
The longest dial code wins, so +1268… is Antigua rather than the US.
Several countries share some codes (+1, +7, +39); among those,
the first of preferredCodes that matches wins, then the first in
all.
Implementation
static VoCountry? byPhone(
String phone, {
Iterable<String> preferredCodes = const [],
}) {
final normalized = _normalizeDialCode(phone);
VoCountry? best;
for (final country in all) {
final dial = country.e164DialCode;
if (!normalized.startsWith(dial)) continue;
if (best == null || dial.length > best.e164DialCode.length) {
best = country;
}
}
if (best == null) return null;
final tied = all.where((c) => c.e164DialCode == best!.e164DialCode);
for (final code in preferredCodes) {
final match = tied.where((c) => c.code == code).firstOrNull;
if (match != null) return match;
}
return best;
}