Site icon Flutter Packages | Pub dev Packages – Flutter Mobile App World

Dart phone number parser, based on libphonenumber and PhoneNumberKit

Phone Numbers Parser

Dart library for parsing phone numbers. Inspired by Google’s libphonenumber and PhoneNumberKit for ios.

The advantage of this lib instead of libphonenumber is that it instantly supports all platforms (no need for channeling).

Features

Parsers

There are two parsers: LightPhoneParser and Phone Parser

LightPhoneParser:

PhoneParser:

Usage PhoneParser

  final parser = PhoneParser();
  // creation
  final frPhone = parser.parseRaw('+33 655 5705 76');
  final frPhone1 = parser.parseWithIsoCode('fr', '655 5705 76');
  final frPhone2 = parser.parseWithDialCode('33', '655 5705 76');
  final frPhone3 = parser.parseWithIsoCode('fr', '0655 5705 76');
  final allSame =
      frPhone == frPhone1 && frPhone == frPhone2 && frPhone == frPhone3;
  print(allSame); // true

  // validation
  print(parser.validate(frPhone1)); // true
  print(parser.validate(frPhone1, PhoneNumberType.mobile)); // true
  print(parser.validate(frPhone1, PhoneNumberType.fixedLine)); // false

  // changing the country
  final esPhone = parser.copyWithIsoCode(frPhone, 'ES');
  print(esPhone.dialCode); // 34
  print(esPhone.isoCode); // ES
  print(esPhone.international); // '+34655570576'

  // utils
  final text = 'hey my phone number is: +33 939 876 218';
  final found = parser.findPotentialPhoneNumbers(text);
  print(text.substring(found.first.start, found.first.end)); 

Usage LightPhoneParser

  final parser = LightPhoneParser();
  // creation
  final frPhone = parser.parseWithIsoCode('fr', '+33 655 5705 76');
  final frPhone1 = parser.parseWithIsoCode('fr', '655 5705 76');
  final frPhone2 = parser.parseWithIsoCode('fr', '655 5705 76');
  final frPhone3 = parser.parseWithIsoCode('fr', '0655 5705 76');
  final allSame =
      frPhone == frPhone1 && frPhone == frPhone2 && frPhone == frPhone3;
  print(allSame); // true

  // validation
  print(parser.validate(frPhone1)); // true
  print(parser.validate(frPhone1, PhoneNumberType.mobile)); // true
  print(parser.validate(frPhone1, PhoneNumberType.fixedLine)); // false

  // changing the country
  final esPhone = parser.copyWithIsoCode(frPhone, 'ES');
  print(esPhone.dialCode); // 34
  print(esPhone.isoCode); // ES
  print(esPhone.international); // '+34655570576'

  // utils
  final text = 'hey my phone number is: +33 939 876 218';
  final found = parser.findPotentialPhoneNumbers(text);
  print(text.substring(found.first.start, found.first.end));

 

Migration to 1.0.0

1.0.0 introduces two parsers:

With this change a few breaking changes had to be made

before:

final frPhone = PhoneNumber.fromRaw('+33 655 5705 76');
final frPhone1 = PhoneNumber.fromIsoCode('FR', '655 5705 76');
final valid = frPhone.validate();

after:

final frPhone = PhoneParser.fromRaw('+33 655 5705 76')
final frPhone1 = LightPhoneParser.fromIsoCode('FR', '655 5705 76');
final valid = PhoneParser.validate(frPhone);

Demo

The phone number input packages has a demo that uses this parser: https://cedvdb.github.io/phone_form_field/

Download Phone Numbers Parser source code on GitHub

https://github.com/cedvdb/phone_numbers_parser
Exit mobile version