command method Null safety

Future<BaseResponse?> command(
  1. String command,
  2. [Map<String, dynamic>? args]
)

This is a helper method for sending commands over the websocket. A SimpleResponse is returned. The function requires a command from the documented list of websocket and optionally args can be provided if required by the command. If OBS returns an error in the response, then an Exception will be thrown.

Implementation

Future<BaseResponse?> command(String command,
    [Map<String, dynamic>? args]) async {
  BaseResponse? response;

  var messageId = sendCommand({'request-type': command}, args);

  await for (String message in broadcast) {
    response = BaseResponse.fromJson(jsonDecode(message));

    if (!response.status && response.messageId == messageId) {
      throw Exception(
          '\nServer returned error to [$command] request:\n\t ${response.error}\n');
    }

    if (response.messageId == messageId) {
      break;
    }
  }

  return response;
}