fernan542 / fl_country_code_picker

A Flutter package for showing a modal that contains country dial code. The user can also search for the available codes and select right from the modal.
MIT License
19 stars 34 forks source link

A way for search and get Country Code without any manual action. #28

Closed MojtabaMohammadzadeh closed 9 months ago

MojtabaMohammadzadeh commented 9 months ago

I needed that find a CountryCode befor user select anythings by a default number. then I add this function in fl_country_code_picker.dart

Future<CountryCode?> getByCode(String CodeNumber) async{ final allCountryCodes = codes.map(CountryCode.fromMap).toList(); for (var element in allCountryCodes) {

 if (element.dialCode == CodeNumber) {
   return element;
 }

} return null; // Return null if the code is not found/ Return null if the code is not found }

it works well for me . I used it in my code like this CountryCode? code =await countryPicker.getByCode(firstDigit);

fernan542 commented 9 months ago

Hello @MojtabaMohammadzadeh 👋 The CountryCode model already have that functionality, added at v1.0.7.

Example:

final phCountryCode = CountryCode.fromCode('PH');
if (phCountryCode == null) print("Ph country code not found!");

You also have the other convenient methods such as; fromName, fromDialCode

MojtabaMohammadzadeh commented 9 months ago

Thank you very much. It realy helped me . You are fantastic.

fernan542 commented 9 months ago

Cool, I'm glad to hear that! :)