flutter-libphonenumber icon indicating copy to clipboard operation
flutter-libphonenumber copied to clipboard

isValidPhoneNumber throws exception instead of returning false

Open elvisun opened this issue 5 years ago • 4 comments

When I passed "+1" to isValidPhoneNumber, it throws the following exception:

E/flutter (13120): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(NumberParseException, The string supplied did not seem to be a phone number., null)

isn't the point to return false when it's not a phone number?

I'm planning on adding a try catch block around this function, happy to submit a PR if approved, does that make sense?

elvisun avatar Mar 20 '20 02:03 elvisun

Yes, you are right. This function should never throw an exception, but only return the boolean value.

If you could create a PR for it, I would appreciate it a lot!

emostar avatar Apr 02 '20 07:04 emostar

Thanks! PR in https://github.com/emostar/flutter-libphonenumber/pull/18

elvisun avatar Apr 02 '20 14:04 elvisun

Hi @emostar, there is a similar issue for PhoneNumberUtil.getRegionInfo and .normalizePhoneNumber when using an invalid phone number. Is this expected behaviour? Should we validate the phone number before trying to call these methods, or should it return a default value on PlatformException?

ClemaX avatar Oct 01 '20 10:10 ClemaX

Yeah I think getRegionInfo and normalizePhoneNumber needs the number to be valid, so throwing an exception is proper. You can either call isValidPhoneNumber first or have a catch block yourself to handle the exception (e.g. present error message back to user).

elvisun avatar Oct 01 '20 13:10 elvisun