intl_phone_field
intl_phone_field copied to clipboard
initialCountryCode do not work with country dial code
When we put dial code in initialCountryCode, the result is not correct :
For example :
IntlPhoneField( initialCountryCode: '+33', )
=> The result will show a initial country code to +93 (first country in the countries list).
If we put the ISO code, it work as expected.
I think that the solution should be to change the following code in intl_phone_field.dart :
Line 319 :
else {
_selectedCountry = _countryList.firstWhere((item) => item.code == (widget.initialCountryCode ?? 'US'),
orElse: () => _countryList.first);
// remove country code from the initial number value
.....
By this one :
else {
if(widget.initialCountryCode != null && widget.initialCountryCode!.startsWith('+')) {
_selectedCountry = _countryList.firstWhere((item) => item.dialCode == widget.initialCountryCode!.substring(1),
orElse: () => _countryList.first);
} else {
_selectedCountry = _countryList.firstWhere((item) => item.code == (widget.initialCountryCode ?? 'US'),
orElse: () => _countryList.first);
}
// remove country code from the initial number value
....
getCountryCodeFromDialCode(String? dialCode){ if(dialCode==null) return 'UG'; var _selectedCountry = countries.firstWhere((item) => item.dialCode == dialCode.replaceAll("+", '')); return _selectedCountry.code; }
'countries' list is provided in the package itself.