how to initiate phone number on intl phone number package

Issue

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?

Solution

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");
    }

Answered By – Zakaria Hossain

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.