acoutts / flutter_libphonenumber

Leverages libphonenumber to allow for asynchronous and synchronous formatting of phone numbers in Flutter apps. Includes a TextInputFormatter to allow real-time AsYouType formatting.
MIT License
59 stars 72 forks source link

Facing issue in formatting number #71

Open arslankaleem7229 opened 2 months ago

arslankaleem7229 commented 2 months ago

I am retrieving contacts from my phone and parsing it using formatNumberSync function to format phone number now I am facing an issue with country codes. Let suppose some of my contacts have country code in it and some contacts don't have country code in it. How you handle this thing? As the number being parsed it plain String lie "1234567890" or "+11234567980". Now how can I handle this thing.

Thanks!

acoutts commented 2 months ago

It's a tricky thing. There's no real solution since you have to know the country code to correctly parse it. The best you can do is guess which is what the package tries to do.

A safe approach is to default to the device locale to get the country code, because you can reasonably assume local numbers to the device probably don't have the country code saved for most cases.