phone_numbers_parser icon indicating copy to clipboard operation
phone_numbers_parser copied to clipboard

National phone number seems to not be correctly formatted

Open geosebas opened this issue 11 months ago • 5 comments

Hello,

I'm French so I will only talk about french number here but when I try to get a formatted national number, I don't get the correct format.

Here an example :

    final parsedPhone = PhoneNumber.parse('+33611223344', callerCountry: IsoCode.FR, destinationCountry: IsoCode.FR);
    debugPrint('nsn : ${parsedPhone.nsn}'); // print 611223344
    debugPrint('international : ${parsedPhone.international}'); // print +33611223344
    debugPrint('countryCode : ${parsedPhone.countryCode}'); // print 33
    debugPrint('national nsn formatted : ${parsedPhone.formatNsn(format: NsnFormat.national)}'); // print 6 11 22 33 44
    debugPrint('international nsn formatted : ${parsedPhone.formatNsn(format: NsnFormat.international)}'); // print 6 11 22 33 44

My problem here is that I should have a function that allow me to display a formatted national number like :

final nsn = '06 11 22 33 44' // that how a french formatted national number should look 

For french number, the country code '+33' is replaced by a '0'.

Do you have any solution for this problem ?

Edit : I don't know if it's a bug related to your metadata, of it's by design that you want to display the number like that. If it's by design, do you think we have a way to implement a method to the parser that will allow that ? Edit 2 : I tested for belgium phone number and I got the same problem. I specify that I'm talking only about mobile phone number here

geosebas avatar Mar 20 '25 16:03 geosebas

Here a example using this code with the flutter_libphonenumber package

FutureBuilder(
  future: parse('+33611223344'),
  builder: (context, snapshot) {
    if (snapshot.hasError) {
      return Center(
        child: Text(snapshot.error.toString()),
      );
    }
    if (!snapshot.hasData) {
      return Container();
    }
    final data = snapshot.data!;
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(data['country_code']), // 33
          Text(data['e164']), // +33611223344
          Text(data['national']), // 06 11 22 33 44
          Text(data['type']), // mobile
          Text(data['international']), // +33 6 11 22 33 44
          Text(data['region_code']), // FR
          Text(data['national_number']), // 611223344
        ],
      ),
    );
  },
)

geosebas avatar Mar 20 '25 17:03 geosebas

I'm from belgium the rules are similar. In a phone number input though, one may want to display the national format like this:

[ +32 (country selector) ] [ 479 22 88 33 ]

with the leading 0 formatted out.

What do you propose so those two use cases (yours and the one above) are supported ?

I'd suggest deprecating the formatNsn method in favor of a more general format method with the following formats:

  • e164
  • international
  • national - 06 11 22 33 44
  • another name for the format maybe "nsn" - 6 11 22 33 44

cedvdb avatar Mar 22 '25 08:03 cedvdb

It would be nice if the package includes format() -> [e164, national, international]

You could keep formatNsn() for a generalized NSN format


Or formatNsn() returns the nsn with the national leading prefix

natintosh avatar Apr 17 '25 07:04 natintosh

Hello,

Sorry I didn't see the notification for your response.

I'd would say that deprecating the formatNsn is better for consistency, it would help the new user to only have a format method with a simple enum as a parameter.

As for current user, just having a deprecated message clear enough and enough time to update the codebase should be ok imo

geosebas avatar Apr 17 '25 12:04 geosebas

ir's settled then. Feel free to add it, I'll review the PR

cedvdb avatar Apr 18 '25 20:04 cedvdb