handleAs<T> method

T handleAs<T>({
  1. T simple(
    1. RespSimpleString
    )?,
  2. T bulk(
    1. RespBulkString
    )?,
  3. T integer(
    1. RespInteger
    )?,
  4. T array(
    1. RespArray
    )?,
  5. T error(
    1. RespError
    )?,
})

Calls one of the given handlers based on the concrete type. Returns true if a handler for the concrete type was provided, otherwise false is returned. If the handler throws an error while executing, the error is raised to the caller of this method.

Implementation

T handleAs<T>({
  T Function(RespSimpleString)? simple,
  T Function(RespBulkString)? bulk,
  T Function(RespInteger)? integer,
  T Function(RespArray)? array,
  T Function(RespError)? error,
}) {
  if (isSimpleString && simple != null) {
    return simple(toSimpleString());
  } else if (isBulkString && bulk != null) {
    return bulk(toBulkString());
  } else if (isInteger && integer != null) {
    return integer(toInteger());
  } else if (isArray && array != null) {
    return array(toArray());
  } else if (isError && error != null) {
    return error(toError());
  }
  throw ArgumentError('No handler provided for type $typeName!');
}