lookup method Null safety

Future<String> lookup(
  1. String domain,
  2. [LookupOptions options = const LookupOptions()]
)

Lookups whois information on a particular domain name.

Implementation

static Future<String> lookup(
  String domain, [
  LookupOptions options = const LookupOptions(),
]) async {
  // Getting whois server information
  final whoisServer = _serverDataByName(domain);
  if (whoisServer.isEmpty) {
    throw ArgumentError('Unsupported TLD');
  }

  final domainQuery = whoisServer['query'] == null
      ? domain
      : whoisServer['query'].replaceAll('{name}', domain);

  final Completer<String> completer = Completer<String>();

  // Create socket
  final socket = await Socket.connect(
    whoisServer['server'],
    options.port,
    timeout: options.timeout,
  );

  // Listen for responses from the server
  socket.listen(
    // Handle data from the server
    (Uint8List data) {
      final serverResponse = String.fromCharCodes(data);
      completer.complete(serverResponse);
    },

    // Handle errors
    onError: (error) {
      socket.destroy();
      throw const SocketException('Failed to lookup');
    },

    // Handle server ending connection
    onDone: () {
      socket.destroy();
    },
  );

  // Send initial query for whois record
  socket.write(domainQuery + _CRLF);

  return completer.future;
}