Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialCountryCode do not work with country dial code #377

Open
sebNiji opened this issue Apr 15, 2024 · 2 comments
Open

initialCountryCode do not work with country dial code #377

sebNiji opened this issue Apr 15, 2024 · 2 comments

Comments

@sebNiji
Copy link

sebNiji commented Apr 15, 2024

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.

@sebNiji
Copy link
Author

sebNiji commented Apr 15, 2024

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
      ....

@razaen801
Copy link

razaen801 commented Aug 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants