setMessageHandler method

void setMessageHandler (Future<T> handler(T message))

Sets a callback for receiving messages from the platform plugins on this channel. Messages may be null.

The given callback will replace the currently registered callback for this channel, if any. To remove the handler, pass null as the handler argument.

The handler's return value is sent back to the platform plugins as a message reply. It may be null.

Implementation

void setMessageHandler(Future<T> handler(T message)) {
  if (handler == null) {
    BinaryMessages.setMessageHandler(name, null);
  } else {
    BinaryMessages.setMessageHandler(name, (ByteData message) async {
      return codec.encodeMessage(await handler(codec.decodeMessage(message)));
    });
  }
}