setMockMessageHandler method

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

Sets a mock callback for intercepting messages sent on this channel. Messages may be null.

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

The handler's return value is used as a message reply. It may be null.

This is intended for testing. Messages intercepted in this manner are not sent to platform plugins.

Implementation

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