setMethodCallHandler method
- Future handler(
- MethodCall call
Sets a callback for receiving method calls on this channel.
The given callback will replace the currently registered callback for this
channel, if any. To remove the handler, pass null as the
handler
argument.
If the future returned by the handler completes with a result, that value is sent back to the platform plugin caller wrapped in a success envelope as defined by the codec of this channel. If the future completes with a PlatformException, the fields of that exception will be used to populate an error envelope which is sent back instead. If the future completes with a MissingPluginException, an empty reply is sent similarly to what happens if no method call handler has been set. Any other exception results in an error envelope being sent.
Implementation
void setMethodCallHandler(Future<dynamic> Function(MethodCall call)? handler) {
assert(
_binaryMessenger != null || BindingBase.debugBindingType() != null,
'Cannot set the method call handler before the binary messenger has been initialized. '
'This happens when you call setMethodCallHandler() before the WidgetsFlutterBinding '
'has been initialized. You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() '
'before this or by passing a custom BinaryMessenger instance to MethodChannel().',
);
binaryMessenger.setMessageHandler(
name,
handler == null
? null
: (ByteData? message) => _handleAsMethodCall(message, handler),
);
}