parseKey static method
Parse a key string into address and port Supports both IPv4 and IPv6 formats:
- IPv4: "192.168.1.100:8080"
- IPv6: "
2001:db8::1:8080" Returns null if the format is invalid
Implementation
static ({String address, int port})? parseKey(String key) {
// Check for IPv6 format with brackets
if (key.startsWith('[')) {
final closeBracket = key.indexOf(']');
if (closeBracket == -1) return null;
final ipv6Address = key.substring(1, closeBracket);
final portPart = key.substring(closeBracket + 1);
if (!portPart.startsWith(':')) return null;
final port = int.tryParse(portPart.substring(1));
if (port == null) return null;
return (address: ipv6Address, port: port);
}
// IPv4 format
final lastColon = key.lastIndexOf(':');
if (lastColon == -1) return null;
final address = key.substring(0, lastColon);
final port = int.tryParse(key.substring(lastColon + 1));
if (port == null) return null;
return (address: address, port: port);
}