connect static method Null safety

Future<MySqlConnection> connect(
  1. ConnectionSettings c,
  2. {bool isUnixSocket = false}

Connects a MySQL server at the given host on port, authenticates using user and password and connects to db.

c.timeout is used as the connection timeout and the default timeout for all socket communication.

A SocketException is thrown on connection failure or a socket timeout connecting the socket. A TimeoutException is thrown if there is a timeout in the handshake with the server.


static Future<MySqlConnection> connect(ConnectionSettings c, {bool isUnixSocket = false}) async {
  assert(!c.useSSL); // Not implemented

  ReqRespConnection? conn;
  late Completer handshakeCompleter;

  _log.fine('opening connection to ${}:${c.port}/${c.db}');

  var socket = await BufferedSocket.connect(, c.port, c.timeout, isUnixSocket: isUnixSocket, onDataReady: () {
  }, onDone: () {
  }, onError: (Object error) {
    _log.warning('socket error: $error');

    // If conn has not been connected there was a connection error.
    if (conn == null) {
    } else {
  }, onClosed: () {
    if (conn != null) {

  Handler handler =
      HandshakeHandler(c.user, c.password, c.maxPacketSize, c.characterSet, c.db, c.useCompression, c.useSSL);
  handshakeCompleter = Completer<void>();
  conn = ReqRespConnection(socket, handler, handshakeCompleter, c.maxPacketSize);

  await handshakeCompleter.future.timeout(c.timeout);
  return MySqlConnection(c.timeout, conn);