Reputation: 389
i have a string got it from database for example "+96170123456"
but the phone number in not always like above, it is international,
so how to put it in intl phone number
phoneNbr = "+96170123456";
PhoneNumber number = PhoneNumber(phoneNumber: phoneNbr, isoCode: 'LB');
InternationalPhoneNumberInput(
onInputChanged: (PhoneNumber number) {
print(number.phoneNumber);
},
onInputValidated: (bool value) {
print(value);
},
ignoreBlank: false,
initialValue: number,
)
i use intl_phone_number_input: ^0.7.0+2
so how to know the iso code programatically?? what do u suggest me?
Upvotes: 0
Views: 2933
Reputation: 321
If you can modify what is stored in the database... then the easiest solution I could come up with is to store the countryDialCode
also.
Then you can easily get the initialValue
by removing the country dial code.
String phoneNumber = "+96170123456"; // from database
String countryDialCode = '961'; // from database
String number = phoneNumber.replaceFirst('+${countryDialCode}', '');
This will give you number = 70123456
which will now appear as the initial value.
Incase it still does not appear - try using a TextEditingController
instead of initialValue
.
For anyone else reading and If I understand correctly - the issue is that if you try to place +96170123456
as the initial value, nothing will appear as this string contains the country code. So the country code needs to be removed but it's hard to do so without knowing which country the number belongs to.
Upvotes: 0
Reputation: 86
I don't know what you really want cause the question is not yet clear to me but I think this should work
Use this in the initState:
final List<Locale> systemLocales=WidgetsBinding.instance!.window.locales;
String? isoCountryCode = systemLocales.first.countryCode;
or you can use this other one but in the Build context:
Locale myLocale = Localizations.localeOf(context);
print(myLocale.countryCode);
This will get the country code and you can add it like this:
PhoneNumber number = PhoneNumber(phoneNumber: phoneNbr, isoCode: '${myLocale.countryCode}');
Upvotes: 0
Reputation: 2504
You can use getRegionInfoFromPhoneNumber
method to check that this phone number is a valid number or not.
try {
var phoneNumber = "+234 500 500 5005";
PhoneNumber number = await PhoneNumber.getRegionInfoFromPhoneNumber(phoneNumber);
InternationalPhoneNumberInput(
onInputChanged: (PhoneNumber number) {
print(number.phoneNumber);
},
onInputValidated: (bool value) {
print(value);
},
ignoreBlank: false,
initialValue: number,
);
} catch(e) {
log("This phone number is not a valid number");
}
Upvotes: 2