isValidIpv4 function

bool isValidIpv4(
  1. String host
)

Returns true when host is a syntactically valid IPv4 address: four dot-separated octets, each a decimal number in the range 0..255 with no leading sign and at most three digits.

Implementation

bool isValidIpv4(String host) {
  final parts = host.split('.');
  if (parts.length != 4) return false;
  for (final part in parts) {
    if (part.isEmpty || part.length > 3) return false;
    final n = int.tryParse(part);
    if (n == null || n < 0 || n > 255) return false;
  }
  return true;
}