Dart NetworkInterface
last modified October 18, 2026
The NetworkInterface class in Dart provides access to network
interface information. It allows querying available network interfaces
and their addresses.
NetworkInterface is part of Dart's dart:io library. It's useful
for network programming, server configuration, and network diagnostics.
Basic Definition
NetworkInterface represents a network interface available on
the host system. Each interface has a name and list of associated addresses.
Key properties include the interface name and its list of addresses; each address carries its type (IPv4 or IPv6). The class provides a static method to list all interfaces.
Listing All Network Interfaces
This example shows how to list all available network interfaces.
import 'dart:io';
void main() async {
try {
List<NetworkInterface> interfaces =
await NetworkInterface.list(includeLoopback: true);
for (var interface in interfaces) {
print('Interface: ${interface.name}');
for (var addr in interface.addresses) {
print(' ${addr.address} (${addr.type.name})');
}
}
} catch (e) {
print('Error: $e');
}
}
We use NetworkInterface.list() to get all interfaces. Loopback interfaces are excluded by default, so we pass includeLoopback: true. This returns a Future that we await. For each interface, we print its name and associated addresses.
$ dart main.dart Interface: eth0 192.168.1.100 (IPv4) Interface: lo 127.0.0.1 (IPv4) ::1 (IPv6)
The interface names and addresses above are examples; the actual values depend on your system.
Filtering by Interface Type
This example demonstrates filtering interfaces by address type.
import 'dart:io';
void main() async {
var interfaces = await NetworkInterface.list(
includeLoopback: false,
type: InternetAddressType.IPv4
);
print('Non-loopback IPv4 interfaces:');
for (var interface in interfaces) {
print('${interface.name}:');
interface.addresses.forEach((addr) => print(addr.address));
}
}
We filter interfaces to exclude loopback and only show IPv4 addresses. The
list method accepts parameters to customize the returned interface
list.
$ dart main.dart Non-loopback IPv4 interfaces: eth0: 192.168.1.100 wlan0: 10.0.0.15
The number and names of interfaces vary between machines.
Checking for Specific Address
This example checks if a specific IP address exists on any interface.
import 'dart:io';
void main() async {
const targetIp = '127.0.0.1';
bool found = false;
var interfaces = await NetworkInterface.list(includeLoopback: true);
for (var interface in interfaces) {
for (var addr in interface.addresses) {
if (addr.address == targetIp) {
found = true;
print('$targetIp found on ${interface.name}');
break;
}
}
if (found) break;
}
if (!found) {
print('$targetIp not found on any interface');
}
}
We search all interfaces for a specific IP address. This is useful for verifying network configuration or checking interface assignments programmatically.
$ dart main.dart 127.0.0.1 found on lo
Getting Interface by Name
This example shows how to get details for a specific interface by name.
import 'dart:io';
void main() async {
const interfaceName = 'lo';
var interfaces = await NetworkInterface.list(includeLoopback: true);
var matches = interfaces.where((i) => i.name == interfaceName);
if (matches.isNotEmpty) {
var targetInterface = matches.first;
print('Details for $interfaceName:');
print('Addresses:');
targetInterface.addresses.forEach((addr) => print(addr.address));
} else {
print('Interface $interfaceName not found');
}
}
We find an interface by its name and display its addresses. The where method filters the complete list to the matching interface.
$ dart main.dart Details for lo: Addresses: 127.0.0.1 ::1
Monitoring Interface Changes
This example demonstrates monitoring for network interface changes.
import 'dart:io';
import 'dart:async';
void main() async {
print('Initial interfaces:');
var initial = await NetworkInterface.list(includeLoopback: true);
printInterfaces(initial);
print('\nMonitoring for changes (Ctrl+C to stop)...');
Timer.periodic(Duration(seconds: 5), (timer) async {
var current = await NetworkInterface.list(includeLoopback: true);
if (interfacesChanged(initial, current)) {
print('\nInterface change detected:');
printInterfaces(current);
initial = current;
}
});
}
bool interfacesChanged(List<NetworkInterface> a, List<NetworkInterface> b) {
if (a.length != b.length) return true;
for (int i = 0; i < a.length; i++) {
if (a[i].name != b[i].name ||
a[i].addresses.length != b[i].addresses.length) {
return true;
}
}
return false;
}
void printInterfaces(List<NetworkInterface> interfaces) {
for (var interface in interfaces) {
print('${interface.name}:');
interface.addresses.forEach((addr) => print(' ${addr.address}'));
}
}
We periodically check for interface changes by comparing current state with initial state. This can detect network configuration changes at runtime.
$ dart main.dart Initial interfaces: eth0: 192.168.1.100 lo: 127.0.0.1 Monitoring for changes (Ctrl+C to stop)... Interface change detected: eth0: 192.168.1.100 wlan0: 10.0.0.15 lo: 127.0.0.1
Best Practices
- Error handling: Always handle potential exceptions
- Async/await: Use async/await for cleaner code
- Filtering: Filter results when possible for efficiency
- IPv6 support: Consider IPv6 addresses in modern apps
- Platform differences: Be aware of platform-specific behaviors
Source
Dart NetworkInterface Documentation
This tutorial covered Dart's NetworkInterface class with practical examples showing interface listing, filtering, monitoring, and address management.
Author
List all Dart tutorials.