Open netgfx opened 3 years ago
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.
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.
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?
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.
+12024003000
output: +1 (202) 400-3000
(if region is NOT specified)12024003000
output depends on region specified and how libphonenumber or PhoneNumberKit handles it6588887777
output again depends on region+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
.
@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
andPhoneNumberKit
without the massive amount of code in theStore
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.
For this:
await PhoneNumberUtil().parse("+17703228", regionCode: "US");
it returns
(268) 770-2832
for.international
and2687702832
for national. Which doesn't respond to a US number. I would expectregionCode
to make theparse
return invalid/null until a valid US number is provided.Note that I'm running this on text change so it is certainly not a whole number while the user types, but since there is a specific regionCode it shouldn't need to parse the number for other countries