Coder Social home page Coder Social logo

Comments (5)

nashfive avatar nashfive commented on August 17, 2024

Hi @netgfx, sorry for the late reply.
Does this occur on both platforms ?

Please note that "as you type" formatting/parsing is not something that is supported by this plugin...
It should be something that can be added though since it seems that PhoneKit and libphonenumber allows this mechanism. I haven't tried it nor implemented it, since I had no use for it at the time.

Now, about the parsing of your number, I think that it's not linked to the code of this plugin, but more related to how the native libraries are doing the parsing. Maybe when you provide a region, the phone number doesn't expect to parse an international number... But again, the fact that you're doing a partial formatting/parsing, you might get unexpected results.

from phone_number.

netgfx avatar netgfx commented on August 17, 2024

Hello, I have only tested on iOS. The fix was to check if the number had >= 10 digits before attempting a parse. But as I stated I would expect the parsing to fail until a valid US number was provided. The "bug" here is that while I do provide a region it wrongfully attempts to parse "any" number and thus it finds a random international number of a totally unrelated country.

from phone_number.

ooglek avatar ooglek commented on August 17, 2024

Is there any way to better implement type-as-you-go or partial formatting found in both libphonenumber and PhoneNumberKit without the massive amount of code in the Store class?

Many things are good about this library, but lacking solid support from either foundational classes for Partial formatting is causing me to pull out some hair and look for a different library!

Can I help?

from phone_number.

ooglek avatar ooglek commented on August 17, 2024

I figured something better out.

If the phone number starts with a + find the matching code as you type. Seems to work well, though I didn't do performance tests.

It might be nice to build a method that allows you to extract the Region (Country) Code from a full or partial phone number.

e.g. PhoneNumberUtil().getRegionCode(number); Returns String ISO3166-2 Country Code

Adding this and having it run if the region parameter were optional or was passed an invalid value (like XQ or null or empty String '') would likely solve the OP's issue.

When there's a leading + sign, it should indicate that you are specifying a full E.164 phone number and the Region (Country) code is implied and should NOT need to be passed separately.

e.g.

  • input: +12024003000 output: +1 (202) 400-3000 (if region is NOT specified)
  • input: 12024003000 output depends on region specified and how libphonenumber or PhoneNumberKit handles it
  • input: 6588887777 output again depends on region
  • input: +6588887777 output: +65 8888 7777 region is Singapore (SG)

Basically ignore what region is when the phone number leads with +

PhoneNumberUtil pnu = PhoneNumberUtil();
String numClean = '+6588887777'; // Replace with your variable with non-dialable chars removed
List<RegionInfo> regions = await pnu.allSupportedRegions();
      RegionInfo found = regions.firstWhere(
          (ri) =>
              numClean.length > ri.prefix.toString().length &&
              numClean.substring(1, ri.prefix.toString().length + 1) ==
                  ri.prefix.toString(),
          orElse: () {
            return null;
      });
      if (found != null) {
        region = found.code;
      }

I found a much easier and faster way of type-as-you-go or "formatAsYouType" as libphonenumber calls it --

Look at the first answer here: https://stackoverflow.com/questions/60981448/flutter-textformfield-calling-future-from-textinputformatter

I'm just calling PhoneNumberUtil().format(numClean, region); in there and doing the fancy region matching if the number contains a + sign. Otherwise grabbing the region of the carrier, or assuming +1 US.

from phone_number.

nicbn avatar nicbn commented on August 17, 2024

@ooglek I think it'd be better if you opened a new issue for the as-you-type discussion, but

Is there any way to better implement type-as-you-go or partial formatting found in both libphonenumber and PhoneNumberKit without the massive amount of code in the Store class?

I'm not sure I understand here, what do you mean by the Store class? PhoneNumberEditingController does not require any such class, see https://github.com/nashfive/phone_number/blob/master/example/lib/autoformat_page.dart .

The ""as you type" formatting/parsing is not something that is supported" was at the time, now this is provided.

from phone_number.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.