domainName property

String get domainName

Extracts the domain name from a URL. Supports URLs with or without 'www' and different TLDs.

Implementation

String get domainName {
  // Split the URL by '.'
  var parts = host.split('.');

  // Remove 'www' if it exists
  if (parts.isNotEmpty && parts[0] == 'www') {
    parts = parts.sublist(1);
  }

  // Return the first part as the domain name, or an empty string if not found
  return parts.isNotEmpty ? parts[0] : host;
}