Package io.flutter.plugin.common

# Class MethodChannel

java.lang.Object
io.flutter.plugin.common.MethodChannel

public class MethodChannel extends Object
A named channel for communicating with the Flutter application using asynchronous method calls.

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.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `MethodChannel.MethodCallHandler`
  A handler of incoming method calls.
  `static interface`
  `MethodChannel.Result`
  Method call result callback.

- ## Constructor Summary

  Constructors
  Constructor
  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`.
  `MethodChannel(BinaryMessenger messenger, String name, MethodCodec codec, BinaryMessenger.TaskQueue taskQueue)`
  Creates a new channel associated with the specified `BinaryMessenger` and with the specified name and `MethodCodec`.

- ## Method Summary

  Modifier and Type
  Method
  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`
  `resizeChannelBuffer(int newSize)`
  Adjusts the number of messages that will get buffered when sending messages to channels that aren't fully set up yet.
  `void`
  `setMethodCallHandler(MethodChannel.MethodCallHandler handler)`
  Registers a method call handler on this channel.
  `void`
  `setWarnsOnChannelOverflow(boolean warns)`
  Toggles whether the channel should show warning messages when discarding messages due to overflow.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### MethodChannel

    public MethodChannel(@NonNull BinaryMessenger messenger, @NonNull String name)
    Creates a new channel associated with the specified `BinaryMessenger` and with the specified name and the standard `MethodCodec`.
    Parameters:
      `messenger` - a `BinaryMessenger`.
      `name` - a channel name String.

  - ### MethodChannel

    public MethodChannel(@NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MethodCodec codec)
    Creates a new channel associated with the specified `BinaryMessenger` and with the specified name and `MethodCodec`.
    Parameters:
      `messenger` - a `BinaryMessenger`.
      `name` - a channel name String.
      `codec` - a `MessageCodec`.

  - ### MethodChannel

    public MethodChannel(@NonNull BinaryMessenger messenger, @NonNull String name, @NonNull MethodCodec codec, @Nullable BinaryMessenger.TaskQueue taskQueue)
    Creates a new channel associated with the specified `BinaryMessenger` and with the specified name and `MethodCodec`.
    Parameters:
      `messenger` - a `BinaryMessenger`.
      `name` - a channel name String.
      `codec` - a `MessageCodec`.
      `taskQueue` - a `BinaryMessenger.TaskQueue` that specifies what thread will execute the handler. Specifying null means execute on the platform thread. See also `BinaryMessenger.makeBackgroundTaskQueue()`.

- ## Method Details

  - ### invokeMethod

    @UiThread public void invokeMethod(@NonNull String method, @Nullable Object arguments)
    Invokes a method on this channel, expecting no result.
    Parameters:
      `method` - the name String of the method.
      `arguments` - the arguments for the invocation, possibly null.

  - ### invokeMethod

    @UiThread public void invokeMethod(@NonNull String method, @Nullable Object arguments, @Nullable MethodChannel.Result callback)
    Invokes a method on this channel, optionally expecting a result.

    Any uncaught exception thrown by the result callback will be caught and logged.

    Parameters:
      `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.

  - ### setMethodCallHandler

    @UiThread public void setMethodCallHandler(@Nullable MethodChannel.MethodCallHandler handler)
    Registers a method call handler on this channel.

    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.

    Parameters:
      `handler` - a `MethodChannel.MethodCallHandler`, or null to deregister.

  - ### resizeChannelBuffer

    public void resizeChannelBuffer(int newSize)
    Adjusts the number of messages that will get buffered when sending messages to channels that aren't fully set up yet. For example, the engine isn't running yet or the channel's message handler isn't set up on the Dart side yet.

  - ### setWarnsOnChannelOverflow

    public void setWarnsOnChannelOverflow(boolean warns)
    Toggles whether the channel should show warning messages when discarding messages due to overflow. When 'warns' is false the channel is expected to overflow and warning messages will not be shown.