formatPhoneNumber static method

String formatPhoneNumber(
  1. String phoneNumber
)

Formats a phone number for WhatsApp API.

phoneNumber is the phone number to format. Returns a normalized phone number.

Implementation

static String formatPhoneNumber(String phoneNumber) {
  // Remove all non-digits except the + sign
  String normalized = phoneNumber.replaceAll(RegExp(r'[^\d+]'), '');

  // Ensure it has a + prefix if it doesn't start with one
  if (!normalized.startsWith('+')) {
    normalized = '+$normalized';
  }

  return normalized;
}