ZetCode

Dart InternetAddressType

last modified October 18, 2026

The InternetAddressType class in Dart represents different types of IP addresses. It's used with the InternetAddress class to specify address families.

This type is essential for network programming in Dart, helping distinguish between IPv4 and IPv6 addresses. It's part of Dart's dart:io library for server-side applications.

Basic Definition

InternetAddressType has constants for the supported address families: IPv4, IPv6, unix, and any. any is the default used when looking up addresses. Unix domain addresses are available on Linux, macOS, and Android.

The type helps determine address formatting, parsing, and network operations. It's often used when creating or inspecting network connections in Dart.

Checking Address Type

This example shows how to check the type of an InternetAddress.

main.dart
import 'dart:io';

void main() {
  var ipv4 = InternetAddress('192.168.1.1');
  var ipv6 = InternetAddress('2001:0db8:85a3::8a2e:0370:7334');
  
  print('IPv4 type: ${ipv4.type}'); // InternetAddressType: IPv4
  print('IPv6 type: ${ipv6.type}'); // InternetAddressType: IPv6
  
  if (ipv4.type == InternetAddressType.IPv4) {
    print('This is an IPv4 address');
  }
}

We create both IPv4 and IPv6 addresses and check their types using the type property. The type property returns an InternetAddressType value.

$ dart main.dart
IPv4 type: InternetAddressType: IPv4
IPv6 type: InternetAddressType: IPv6
This is an IPv4 address

Creating Specific Address Types

This example demonstrates creating addresses with specific types.

main.dart
import 'dart:io';

void main() {
  var ipv4 = InternetAddress('127.0.0.1', type: InternetAddressType.IPv4);
  var ipv6 = InternetAddress('::1', type: InternetAddressType.IPv6);
  
  print('IPv4: ${ipv4.address}');
  print('IPv6: ${ipv6.address}');
  
  try {
    // Invalid numeric address
    print(InternetAddress('not-an-address'));
  } on ArgumentError catch (e) {
    print('Error: $e');
  }
}

We explicitly specify address types when creating addresses. The type is also inferred from the address format when omitted. An invalid address throws an ArgumentError.

$ dart main.dart
IPv4: 127.0.0.1
IPv6: ::1
Error: Invalid argument(s): Invalid internet address not-an-address

Looking Up Address Types

This example shows checking address types during hostname lookup.

main.dart
import 'dart:io';

Future<void> main() async {
  var addresses = await InternetAddress.lookup('example.com');
  
  for (var addr in addresses) {
    print('Address: ${addr.address}');
    print('Type: ${addr.type}');
    print('Is IPv6: ${addr.type == InternetAddressType.IPv6}');
    print('---');
  }
}

We perform a DNS lookup and examine the types of returned addresses. A hostname can resolve to both IPv4 and IPv6 addresses. The exact addresses and their order depend on DNS.

$ dart main.dart
Address: 172.66.147.243
Type: InternetAddressType: IPv4
Is IPv6: false
---
Address: 104.20.23.154
Type: InternetAddressType: IPv4
Is IPv6: false
---
Address: 2606:4700:10::ac42:93f3
Type: InternetAddressType: IPv6
Is IPv6: true
---
Address: 2606:4700:10::6814:179a
Type: InternetAddressType: IPv6
Is IPv6: true
---

Filtering by Address Type

This example demonstrates filtering addresses by their type.

main.dart
import 'dart:io';

Future<void> main() async {
  var addresses = await InternetAddress.lookup('google.com');
  
  var ipv4Addrs = addresses.where(
    (addr) => addr.type == InternetAddressType.IPv4
  );
  
  var ipv6Addrs = addresses.where(
    (addr) => addr.type == InternetAddressType.IPv6
  );
  
  print('IPv4 addresses:');
  ipv4Addrs.forEach((addr) => print(addr.address));
  
  print('\nIPv6 addresses:');
  ipv6Addrs.forEach((addr) => print(addr.address));
}

We look up addresses for google.com and separate them by type using the where method. This is useful when you need to handle different address types separately. The returned addresses depend on DNS.

$ dart main.dart
IPv4 addresses:
142.251.141.174

IPv6 addresses:
2a00:1450:4014:801::200e

Server Socket with Specific Type

This example shows creating a server socket with a specific address type.

main.dart
import 'dart:io';

Future<void> main() async {
  var ipv6Loopback = InternetAddress('::1', type: InternetAddressType.IPv6);
  
  var server = await ServerSocket.bind(
    ipv6Loopback,
    8080,
  );
  
  print('Server listening on IPv6: ${server.address.address}');
  
  server.listen((Socket socket) {
    socket.write('Hello from IPv6 server!');
    socket.close();
  });
}

We create a server socket that only listens on the IPv6 loopback address. The InternetAddressType ensures we're working with the correct address version.

$ dart main.dart
Server listening on IPv6: ::1

Best Practices

Source

Dart InternetAddressType Documentation

This tutorial covered Dart's InternetAddressType class with practical examples showing type checking, address creation, DNS lookups, and server sockets.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.