maskPhoneNumber method

String maskPhoneNumber()

Masks the phone number based on the country code formatting rules.

This method attempts to use flutter_libphonenumber to format the phone number according to the specific formatting rules for the country code. If that fails, it falls back to the synchronous formatting method.

Returns the formatted phone number string, or the original phone number if formatting fails or the country code is not supported.

Example:

final phone = GrxPhoneNumber(phone: '11987654321', countryCode: '+55');
final masked = await phone.maskPhoneNumber();
// Result: "(11) 98765-4321" for Brazilian format

For US numbers:

final phone = GrxPhoneNumber(phone: '1234567890', countryCode: '+1');
final masked = await phone.maskPhoneNumber();
// Result: "(123) 456-7890" for US format

Implementation

String maskPhoneNumber() {
  if (isEmpty || !hasCountryCode) {
    return phone;
  }

  try {
    // Try to use flutter_libphonenumber for more accurate formatting
    // This is a simplified approach that may need adjustment based on the actual API
    return formatNumberSync(toString());
  } catch (e) {
    // If formatting fails, return the original phone number
    return phone;
  }
}