normalizePhoneDigits function

String? normalizePhoneDigits(
  1. String phone
)

Normalizes a phone string to digits with country code (no leading plus), such as 923001234567.

Accepts local (0300…), international (+92300…, 0092300…), short (300…) and the common +92 (0) 300… / +920300… forms, with spaces, dashes, dots, slashes or parentheses, and Urdu or Arabic-Indic digits.

Returns null when the input is not a Pakistani mobile number on an allocated 03XX prefix.

Implementation

String? normalizePhoneDigits(String phone) {
  var digits = stripSeparators(toAsciiDigits(phone));
  if (digits.startsWith('+')) {
    digits = digits.substring(1);
  } else if (digits.startsWith('00')) {
    digits = digits.substring(2);
  }
  if (digits.startsWith('92') && digits.length > 10) {
    digits = digits.substring(2);
  }
  if (digits.startsWith('0')) {
    digits = digits.substring(1);
  }
  if (!_mobile.hasMatch(digits)) return null;
  if (!_allocatedPrefixes.contains('0${digits.substring(0, 3)}')) return null;
  return '92$digits';
}