fromHost static method

Future<IPAddress> fromHost(
  1. String host, {
  2. InternetAddressType type = InternetAddressType.IPv4,
  3. InternetAddress hostPredicate(
    1. List<InternetAddress>
    )?,
})

Build an IPAddress instance from the host's IP address of type.

By default the function will select and return the first address returned within the lookup. hostPredicate can be set to choose which host address is selected and returned.

Implementation

static Future<IPAddress> fromHost(
  String host, {
  InternetAddressType type = InternetAddressType.IPv4,
  InternetAddress Function(List<InternetAddress>)? hostPredicate,
}) async {
  final result = await InternetAddress.lookup(
    host,
    type: type,
  );

  if (result.isEmpty) {
    throw StateError('No address was associate with the host ($host)');
  }

  final iAddr = hostPredicate?.call(result) ?? result.first;
  return IPAddress(iAddr.address, type: type);
}