cedvdb / phone_numbers_parser

Dart phone number parser, based on libphonenumber and PhoneNumberKit.
https://pub.dev/packages/phone_numbers_parser
MIT License
63 stars 33 forks source link

Allow country selector to have an empty/null value #67

Closed hbatalhaStch closed 5 months ago

hbatalhaStch commented 5 months ago

I just had a case where the phone I got from a database didn't contain a country code and I got an exception that says that the country code was not found and it just crashed.

My suggestion is to allow it to have a null value in case something goes wrong when trying to parse the country code. In other words allow it to handle phone numbers that don't have a country code.

Sample:

final controller = PhoneNumber.parse('9999987"); // this will throw an exception

I would like to initialize the phone field despite not having a country code so that I can visually inform the user that the field doesn't have a country code so maybe he should set it.

cedvdb commented 5 months ago

So yuo want this snippet below to return null ?

PhoneNumber.parse('9999987")

Note that you can urrently use a try catch but parseOrNull could be added

hbatalhaStch commented 5 months ago

So yuo want this snippet below to return null ?

No, I want to want to initialize the phone field but have the country selector empty.

Note that you can urrently use a try catch but parseOrNull could be added

That's what I am currently doing, using try catch, but I am not able to have a phone field with a number where my users can edit the number.

cedvdb commented 5 months ago

What do you want this to return PhoneNumber.parse('9999987"); ?

hbatalhaStch commented 5 months ago

What do you want this to return PhoneNumber.parse('9999987"); ?

That is one the numbers without a country code that is coming from the database. Some have country code and many don't.

cedvdb commented 5 months ago

Can you answer the question ?

hbatalhaStch commented 5 months ago

Sorry I misread the "what" for "why".

What do you want this to return PhoneNumber.parse('9999987"); ?

It would return a PhoneNumber object with a null isoCode or a IsoCode.undefined.

cedvdb commented 5 months ago

Making the isoCode nullable would be a breaking change and I'm not convinced it's warranted here but I could be wrong.

Your issue seem to be with a phone field where you want no country selected at first. I suggest handling that at that level (IE PhoneNumberCandidate wrapper). This might furnish inspiration: https://pub.dev/packages/phone_form_field as this uses this library under the hood

hbatalhaStch commented 5 months ago

Making the isoCode nullable would be a breaking change and I'm not convinced it's warranted here but I could be wrong.

Instead of making it nullable, an IsoCode.undefined could be added.

Your issue seem to be with a phone field where you want no country selected at first

No country selected when isCode is not found in the given number.

I suggest handling that at that level (IE PhoneNumberCandidate wrapper).

I am not sure I understand what you mean.

cedvdb commented 5 months ago

As I said a nullable isocode (or worst a IsoCode.undefined) won't be added currently as this will be a breaking change and there are other ways to achieve what you want. If this is something more people need this will be reconsidered. So I'm closing the issue for now.

I suggest you use a wrapper class for your phone number for now.