getInitials static method

String getInitials(
  1. String? fullName
)

Implementation

static String getInitials(String? fullName) {
  // Return "U" if the full name is null or empty
  if (fullName == null || fullName.trim().isEmpty) return "U";

  // Split the name into parts, ignoring extra spaces
  List<String> nameParts = fullName
      .trim()
      .split(RegExp(r'\s+')) // handles multiple spaces
      .where((part) => part.isNotEmpty)
      .toList();

  // Take only the first alphabet of the first two words
  String initials = nameParts
      .take(2)
      .map((part) => part[0].toUpperCase())
      .join();

  // Ensure it's restricted to 2 characters max
  return initials.length > 2 ? initials.substring(0, 2) : initials;
}