handlePlatformMessage method
- String channel,
- ByteData? data,
- PlatformMessageResponseCallback? callback
Send a mock message to the framework as if it came from the platform.
If a listener has been set using setMessageHandler, that listener is invoked to handle the message, and this method returns a future that completes with that handler's result.
It is strongly recommended that all handlers used with this API be
synchronous (not requiring any microtasks to complete), because
testWidgets tests run in a FakeAsync zone in which microtasks do not
progress except when time is explicitly advanced (e.g. with
WidgetTester.pump), which means that await
ing a Future will result
in the test hanging.
If no listener is configured, this method returns right away with null.
The callback
argument, if non-null, will be called just before this
method's future completes, either with the result of the listener
registered with setMessageHandler, or with null if no listener has
been registered.
Messages can also be sent via ChannelBuffers.push (see ServicesBinding.channelBuffers); the effect is the same, though that API will not wait for a response.
Implementation
// TODO(ianh): When the superclass `handlePlatformMessage` is removed,
// remove this @override (but leave the method).
@override
Future<ByteData?> handlePlatformMessage(
String channel,
ByteData? data,
ui.PlatformMessageResponseCallback? callback,
) {
Future<ByteData?>? result;
if (_inboundHandlers.containsKey(channel)) {
result = _inboundHandlers[channel]!(data);
}
result ??= Future<ByteData?>.value();
if (callback != null) {
result = result.then((ByteData? result) { callback(result); return result; });
}
return result;
}