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.

  • Constructor Details

  • 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.