off method

void off(
  1. String methodName, {
  2. MethodInvocationFunc? method,
})

Removes the specified handler for the specified hub method.

You must pass the exact same Function instance as was previously passed to HubConnection.on. Passing a different instance (even if the function body is the same) will not remove the handler.

methodName: The name of the method to remove handlers for. method: The handler to remove. This must be the same Function instance as the one passed to {@link @microsoft/signalr.HubConnection.on}. If the method handler is omitted, all handlers for that method will be removed.

Implementation

void off(String methodName, {MethodInvocationFunc? method}) {
  if (isStringEmpty(methodName)) {
    return;
  }

  methodName = methodName.toLowerCase();
  final List<void Function(List<Object>)>? handlers = _methods[methodName];
  if (handlers == null) {
    return;
  }

  if (method != null) {
    final removeIdx = handlers.indexOf(method);
    if (removeIdx != -1) {
      handlers.removeAt(removeIdx);
      if (handlers.length == 0) {
        _methods.remove(methodName);
      }
    }
  } else {
    _methods.remove(methodName);
  }
}