public final class MethodChannel extends Object
Incoming method calls are decoded from binary on receipt, and Java results are encoded
into binary before being transmitted back to Flutter. The MethodCodec
used must be
compatible with the one used by the Flutter application. This can be achieved
by creating a
MethodChannel
counterpart of this channel on the Dart side. The Java type of method call arguments and results is
Object
, but only values supported by the specified MethodCodec
can be used.
The logical identity of the channel is given by its name. Identically named channels will interfere with each other's communication.
Modifier and Type | Class and Description |
---|---|
static interface |
MethodChannel.MethodCallHandler
A handler of incoming method calls.
|
static interface |
MethodChannel.Result
Method call result callback.
|
Constructor and Description |
---|
MethodChannel(BinaryMessenger messenger,
String name)
Creates a new channel associated with the specified
BinaryMessenger
and with the specified name and the standard MethodCodec . |
MethodChannel(BinaryMessenger messenger,
String name,
MethodCodec codec)
Creates a new channel associated with the specified
BinaryMessenger and with the
specified name and MethodCodec . |
Modifier and Type | Method and Description |
---|---|
void |
invokeMethod(String method,
Object arguments)
Invokes a method on this channel, expecting no result.
|
void |
invokeMethod(String method,
Object arguments,
MethodChannel.Result callback)
Invokes a method on this channel, optionally expecting a result.
|
void |
setMethodCallHandler(MethodChannel.MethodCallHandler handler)
Registers a method call handler on this channel.
|
public MethodChannel(BinaryMessenger messenger, String name)
BinaryMessenger
and with the specified name and the standard MethodCodec
.messenger
- a BinaryMessenger
.name
- a channel name String.public MethodChannel(BinaryMessenger messenger, String name, MethodCodec codec)
BinaryMessenger
and with the
specified name and MethodCodec
.messenger
- a BinaryMessenger
.name
- a channel name String.codec
- a MessageCodec
.public void invokeMethod(String method, @Nullable Object arguments)
method
- the name String of the method.arguments
- the arguments for the invocation, possibly null.public void invokeMethod(String method, @Nullable Object arguments, MethodChannel.Result callback)
Any uncaught exception thrown by the result callback will be caught and logged.
method
- the name String of the method.arguments
- the arguments for the invocation, possibly null.callback
- a MethodChannel.Result
callback for the invocation result, or null.public void setMethodCallHandler(@Nullable MethodChannel.MethodCallHandler handler)
Overrides any existing handler registration for (the name of) this channel.
If no handler has been registered, any incoming method call on this channel will be handled silently by sending a null reply. This results in a MissingPluginException on the Dart side, unless an OptionalMethodChannel is used.
handler
- a MethodChannel.MethodCallHandler
, or null to deregister.