setMessageHandler method

void setMessageHandler(
  1. Future<T> handler(
    1. 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> Function(T? message)? handler) {
  if (handler == null) {
    binaryMessenger.setMessageHandler(name, null);
  } else {
    binaryMessenger.setMessageHandler(name, (ByteData? message) async {
      return codec.encodeMessage(await handler(codec.decodeMessage(message)));
    });
  }
}