Interface BinaryMessenger
- All Known Implementing Classes:
DartExecutor
BinaryMessenger is expected to be utilized from a single thread throughout the
duration of its existence. If created on the main thread, then all invocations should take place
on the main thread. If created on a background thread, then all invocations should take place on
that background thread.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceHandler for incoming binary messages from Flutter.static interfaceBinary message reply callback.static interfaceAn abstraction over the threading policy used to invoke message handlers.static classOptions that control how a TaskQueue should operate and be created. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidDisables the ability to queue messages received from Dart.default voidEnables the ability to queue messages received from Dart.default BinaryMessenger.TaskQueueCreates a TaskQueue that executes the tasks serially on a background thread.default BinaryMessenger.TaskQueueCreates a TaskQueue that executes the tasks serially on a background thread.voidsend(String channel, ByteBuffer message) Sends a binary message to the Flutter application.voidsend(String channel, ByteBuffer message, BinaryMessenger.BinaryReply callback) Sends a binary message to the Flutter application, optionally expecting a reply.voidsetMessageHandler(String channel, BinaryMessenger.BinaryMessageHandler handler) Registers a handler to be invoked when the Flutter application sends a message to its host platform.default voidsetMessageHandler(String channel, BinaryMessenger.BinaryMessageHandler handler, BinaryMessenger.TaskQueue taskQueue) Registers a handler to be invoked when the Flutter application sends a message to its host platform.
-
Method Details
-
makeBackgroundTaskQueue
Creates a TaskQueue that executes the tasks serially on a background thread.There is no guarantee that the tasks will execute on the same thread, just that execution is serial. This is could be problematic if your code relies on ThreadLocal storage or introspection about what thread is actually executing.
-
makeBackgroundTaskQueue
@UiThread default BinaryMessenger.TaskQueue makeBackgroundTaskQueue(BinaryMessenger.TaskQueueOptions options) Creates a TaskQueue that executes the tasks serially on a background thread.BinaryMessenger.TaskQueueOptionscan be used to configure the task queue to execute tasks concurrently. Doing so can be more performant, though users need to ensure that the task handlers are thread-safe. -
send
Sends a binary message to the Flutter application.- Parameters:
channel- the nameStringof the logical channel used for the message.message- the message payload, a direct-allocatedByteBufferwith the message bytes between position zero and current position, or null.
-
send
@UiThread void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback) Sends a binary message to the Flutter application, optionally expecting a reply.Any uncaught exception thrown by the reply callback will be caught and logged.
- Parameters:
channel- the nameStringof the logical channel used for the message.message- the message payload, a direct-allocatedByteBufferwith the message bytes between position zero and current position, or null.callback- aBinaryMessenger.BinaryReplycallback invoked when the Flutter application responds to the message, possibly null.
-
setMessageHandler
@UiThread void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler) Registers a handler to be invoked when the Flutter application sends a message to its host platform.Registration overwrites any previous registration for the same channel name. Use a null handler to deregister.
If no handler has been registered for a particular channel, any incoming message on that channel will be handled silently by sending a null reply.
- Parameters:
channel- the nameStringof the channel.handler- aBinaryMessenger.BinaryMessageHandlerto be invoked on incoming messages, or null.
-
setMessageHandler
@UiThread default void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler, @Nullable BinaryMessenger.TaskQueue taskQueue) Registers a handler to be invoked when the Flutter application sends a message to its host platform.Registration overwrites any previous registration for the same channel name. Use a null handler to deregister.
If no handler has been registered for a particular channel, any incoming message on that channel will be handled silently by sending a null reply.
- Parameters:
channel- the nameStringof the channel.handler- aBinaryMessenger.BinaryMessageHandlerto be invoked on incoming messages, or null.taskQueue- aBinaryMessenger.TaskQueuethat specifies what thread will execute the handler. Specifying null means execute on the platform thread.
-
enableBufferingIncomingMessages
default void enableBufferingIncomingMessages()Enables the ability to queue messages received from Dart.This is useful when there are pending channel handler registrations. For example, Dart may be initialized concurrently, and prior to the registration of the channel handlers. This implies that Dart may start sending messages while plugins are being registered.
-
disableBufferingIncomingMessages
default void disableBufferingIncomingMessages()Disables the ability to queue messages received from Dart.This can be used after all pending channel handlers have been registered.
-