phone_number 0.11.0 copy "phone_number: ^0.11.0" to clipboard
phone_number: ^0.11.0 copied to clipboard

discontinuedreplaced by: phone_numbers_parser
outdated

Flutter plugin for parsing, formatting and validating international phone numbers.

Phone Number for Flutter #

PhoneNumber is a Flutter plugin that allows you to parse, validate and format international phone numbers.

The plugin uses the native libraries libphonenumber for Android and PhoneNumberKit pod for iOS.

Library Version
libphonenumber 8.12.18
PhoneNumberKit 3.3.3

Usage #

Parsing #

Parse a phone number with region prefix.

String springFieldUSASimple = '+14175555470';
PhoneNumber phoneNumber = await PhoneNumberUtil().parse(springFieldUSASimple);

Parse a phone number with region prefix and dashes.

String springFieldUSA = '+1-417-555-5470';
PhoneNumber phoneNumber = await PhoneNumberUtil().parse(springFieldUSA);

Parse a phone number string without the region prefix. Region required. Country calling codes can be found here

String springFieldUSASimpleNoRegion = '4175555470';
RegionInfo region = RegionInfo('US', 1);
PhoneNumber phoneNumber = await PhoneNumberUtil().parse(springFieldUSASimpleNoRegion, region: region);

Parsing a valid phone number results in a phone number object:

// PhoneNumber{
//  e164: +14175555470,
//  type: PhoneNumberType.FIXED_LINE_OR_MOBILE,
//  international: +1 417-555-5470,
//  national: (417) 555-5470,
//  countryCode: 1,
//  nationalNumber: 4175555470,
//  errorCode: null,
//}

Validating #

Validating a phone number requires both the phone number string and the region country code.

PhoneNumberUtil plugin = PhoneNumberUtil();

String springFieldUSASimpleNoRegion = '4175555470';
RegionInfo region = RegionInfo('US', 1);
bool isValid = await plugin.validate(springFieldUSASimpleNoRegion, region.code);

String springFieldUSASimple = '+14175555470';
bool isValid = await plugin.validate(springFieldUSASimple, region.code);

String springFieldUSA = '+1-417-555-5470';
bool isValid = await plugin.validate(springFieldUSA, region.code);

Formatting #

Phone numbers can also be formatted for the UI to display the number.

String springFieldUSASimpleNoRegion = '4175555470';
RegionInfo region = RegionInfo('US', 1);
String formatted = await PhoneNumberUtil().format(springFieldUSASimpleNoRegion, region.code); // (417) 555-5470

PhoneNumber will not add the country prefix unless the phone number has the prefix

String springFieldUSASimpleNoRegion = '+14175555470';
RegionInfo region = RegionInfo('US', 1);
String formatted = await PhoneNumberUtil().format(springFieldUSASimpleNoRegion, region.code); // +1 (417) 555-5470

Regions #

Fetching regions (country code and prefixes).

List<RegionInfo> regions = await plugin.allSupportedRegions();
// [ RegionInfo { code: IM, prefix: 44 }, RegionInfo { code: LU, prefix: 352 }, ... ]

If you want to display all the flags alongside the regions in your UI region picker, consider having a JSON file instead of using this function. Example JSON file

const List<Map<String, dynamic>> countries = [
  {"name":"Afghanistan","flag":"πŸ‡¦πŸ‡«","code":"AF","dial_code":"+93"},
  {"name":"Γ…land Islands","flag":"πŸ‡¦πŸ‡½","code":"AX","dial_code":"+358"},
  ...
]
185
likes
0
pub points
98%
popularity

Publisher

verified publisherflutterformbuilderecosystem.com

Flutter plugin for parsing, formatting and validating international phone numbers.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, meta

More

Packages that depend on phone_number