Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format a local mobile phone number to a international one?

I am using the contact_service package for flutter to retrieve contacts from a device. But I noticed that when I look at the _contacts[index].phones it holds either mobile or other. I only take the contacts that have a mobiel phone number. But then when I look at the phonenumbers I see that they are all have a different format.

+31XXXXXXXX
+31 6 XXXXXXXX
06-XXXXXXXX
06 XXXXXXXX
6 XXXXXXXX
(045) XXXXXXXX
045 XXXXXXXX

Side Note: I am working on a android simulator with a google account

I would prefer to have them all with a country code so that I can compare them with my firebase auth phonenumber. So my question is what do I do?

Do I format the contact list phonenumber on the client side? Or do I just send it and use a cloud function to somehow figure it out? And how do I do that?

like image 443
anonymous-dev Avatar asked Nov 18 '25 09:11

anonymous-dev


2 Answers

Do I format the contact list phonenumber on the client side?

Or do I just send it and use a cloud function to somehow figure it out?

Client side - a good idea. Especially given the fact that just a local phone number is not enough. You also need to guess the country. And that's easier on the client side where you have more context. It's also better for performance.

And how do I do that?

Probably, that's what you're looking for:

  1. https://github.com/google/libphonenumber
  2. https://pub.dev/packages/phone_number#-example-tab-
like image 126
x00 Avatar answered Nov 21 '25 07:11

x00


I would suggest the following:

  1. Know what country your client is from.
  2. Save the number as is with the name
  3. Try to guess the country code through a process and save all versions
  4. Do search using all versions

Trying to guess the correct number:

  • if it starts with + => it's good
  • else if it starts with 00 => replace 00 with +
  • else use the user's country code, in this case check if the contact name corresponds to other searched user
  • Or ask the user: do you know this person?

You can find countries key code here

I hope this helps, Good luck.

like image 41
aldobaie Avatar answered Nov 21 '25 07:11

aldobaie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!