sendCommand method Null safety

Future<Command?> sendCommand(
  1. Command cmd
)

Implementation

Future<Command?> sendCommand(Command cmd) async {
  if (appState.currentUser == null) {
    //NOTE: some test cases execute async socket data
    logger.e('sendCommand: connection is requred');
    throw ConnectionRequiredError();
  }

  // if (!webSocket.isConnected()) {
  //   logger.e('sendCommand: Websocket connection is closed');
  //   throw WebSocketConnectionClosedError();
  // }

  try {
    await ConnectionManager.readyToExecuteWSRequest();
  } catch (e) {
    _ackTimers.remove(cmd.requestId)?.cancel();
    rethrow;
  }

  try {
    webSocket?.send(cmd.encode());
  } catch (e) {
    _ackTimers.remove(cmd.requestId)?.cancel();
    rethrow;
  }
  // } else {
  //   ackTimers[cmd.requestId].cancel();
  //   ackTimers.removeWhere((key, value) => key == cmd.requestId);
  //   throw WebSocketError();
  // }

  final reqId = cmd.requestId;
  logger.i('Command ${cmd.cmd} Payload ${cmd.payload}');
  if (cmd.isAckRequired && reqId != null) {
    final timer = Timer(Duration(seconds: sdk.options.websocketTimeout), () {
      logger.e('sendCommand: did not receive ack in time');
      throw AckTimeoutError();
    });
    _ackTimers[reqId] = timer;

    final completer = Completer<Command>();
    _completers[reqId] = completer;
    return completer.future;
  } else {
    return null;
  }
}