InternetAddress constructor

InternetAddress(
  1. String address, {
  2. InternetAddressType? type,
})

Creates a new InternetAddress from a numeric address or a file path.

If type is InternetAddressType.IPv4, address must be a numeric IPv4 address (dotted-decimal notation). If type is InternetAddressType.IPv6, address must be a numeric IPv6 address (hexadecimal notation). If type is InternetAddressType.unix, address must be a a valid file path. If type is omitted, address must be either a numeric IPv4 or IPv6 address and the type is inferred from the format.

To create a Unix domain address, type should be InternetAddressType.unix and address should be a string.

Implementation

factory InternetAddress(String address, {InternetAddressType? type}) {
  if (type == InternetAddressType.unix) {
    if (!address.startsWith('/')) {
      throw ArgumentError.value(address, 'address');
    }
    return InternetAddress._(
      address: address,
      rawAddress: Uint8List(0),
      type: InternetAddressType.unix,
    );
  }
  final parsed = tryParse(address);
  if (parsed == null) {
    throw ArgumentError.value(address, 'address');
  }
  return parsed;
}