public class DartExecutor extends Object implements BinaryMessenger
To specify a top-level Dart function to execute, use a DartExecutor.DartEntrypoint
to tell DartExecutor
where to find the Dart code to execute, and which Dart function to use as the
entrypoint. To execute the entrypoint, pass the DartExecutor.DartEntrypoint
to executeDartEntrypoint(DartEntrypoint)
.
To specify a Dart callback to execute, use a DartExecutor.DartCallback
. A given Dart callback must
be registered with the Dart VM to be invoked by a DartExecutor
. To execute the callback,
pass the DartExecutor.DartCallback
to executeDartCallback(DartCallback)
.
Once started, a DartExecutor
cannot be stopped. The associated Dart code will execute
until it completes, or until the FlutterEngine
that owns this
DartExecutor
is destroyed.
Modifier and Type | Class and Description |
---|---|
static class |
DartExecutor.DartCallback
Configuration options that specify which Dart callback function is executed and where to find
that callback and other assets required for Dart execution.
|
static class |
DartExecutor.DartEntrypoint
Configuration options that specify which Dart entrypoint function is executed and where to find
that entrypoint and other assets required for Dart execution.
|
BinaryMessenger.BinaryMessageHandler, BinaryMessenger.BinaryReply
Constructor and Description |
---|
DartExecutor(FlutterJNI flutterJNI,
AssetManager assetManager) |
Modifier and Type | Method and Description |
---|---|
void |
executeDartCallback(DartExecutor.DartCallback dartCallback)
Starts executing Dart code based on the given
dartCallback . |
void |
executeDartEntrypoint(DartExecutor.DartEntrypoint dartEntrypoint)
Starts executing Dart code based on the given
dartEntrypoint . |
BinaryMessenger |
getBinaryMessenger()
Returns a
BinaryMessenger that can be used to send messages to, and receive messages
from, Dart code that this DartExecutor is executing. |
String |
getIsolateServiceId()
Returns an identifier for this executor's primary isolate.
|
int |
getPendingChannelResponseCount()
Returns the number of pending channel callback replies.
|
boolean |
isExecutingDart()
Is this
DartExecutor currently executing Dart code? |
void |
notifyLowMemoryWarning()
Notify the Dart VM of a low memory event, or that the application is in a state such that now
is an appropriate time to free resources, such as going to the background.
|
void |
onAttachedToJNI()
Invoked when the
FlutterEngine that owns this DartExecutor attaches to JNI. |
void |
onDetachedFromJNI()
Invoked when the
FlutterEngine that owns this DartExecutor detaches from JNI. |
void |
send(String channel,
ByteBuffer message)
Deprecated.
Use
getBinaryMessenger() instead. |
void |
send(String channel,
ByteBuffer message,
BinaryMessenger.BinaryReply callback)
Deprecated.
Use
getBinaryMessenger() instead. |
void |
setIsolateServiceIdListener(io.flutter.embedding.engine.dart.DartExecutor.IsolateServiceIdListener listener)
Set a listener that will be notified when an isolate identifier is available for this
executor's primary isolate.
|
void |
setMessageHandler(String channel,
BinaryMessenger.BinaryMessageHandler handler)
Deprecated.
Use
getBinaryMessenger() instead. |
public DartExecutor(@NonNull FlutterJNI flutterJNI, @NonNull AssetManager assetManager)
public void onAttachedToJNI()
FlutterEngine
that owns this DartExecutor
attaches to JNI.
When attached to JNI, this DartExecutor
begins handling 2-way communication to/from
the Dart execution context. This communication is facilitate via 2 APIs:
BinaryMessenger
, which sends messages to Dart
PlatformMessageHandler
, which receives messages from Dart
public void onDetachedFromJNI()
FlutterEngine
that owns this DartExecutor
detaches from JNI.
When detached from JNI, this DartExecutor
stops handling 2-way communication to/from
the Dart execution context.
public boolean isExecutingDart()
DartExecutor
currently executing Dart code?public void executeDartEntrypoint(@NonNull DartExecutor.DartEntrypoint dartEntrypoint)
dartEntrypoint
.
See DartExecutor.DartEntrypoint
for configuration options.
dartEntrypoint
- specifies which Dart function to run, and where to find itpublic void executeDartCallback(@NonNull DartExecutor.DartCallback dartCallback)
dartCallback
.
See DartExecutor.DartCallback
for configuration options.
dartCallback
- specifies which Dart callback to run, and where to find it@NonNull public BinaryMessenger getBinaryMessenger()
BinaryMessenger
that can be used to send messages to, and receive messages
from, Dart code that this DartExecutor
is executing.@Deprecated @UiThread public void send(@NonNull String channel, @Nullable ByteBuffer message)
getBinaryMessenger()
instead.BinaryMessenger
send
in interface BinaryMessenger
channel
- the name String
of the logical channel used for the message.message
- the message payload, a direct-allocated ByteBuffer
with the message
bytes between position zero and current position, or null.@Deprecated @UiThread public void send(@NonNull String channel, @Nullable ByteBuffer message, @Nullable BinaryMessenger.BinaryReply callback)
getBinaryMessenger()
instead.BinaryMessenger
Any uncaught exception thrown by the reply callback will be caught and logged.
send
in interface BinaryMessenger
channel
- the name String
of the logical channel used for the message.message
- the message payload, a direct-allocated ByteBuffer
with the message
bytes between position zero and current position, or null.callback
- a BinaryMessenger.BinaryReply
callback invoked when the Flutter application responds to
the message, possibly null.@Deprecated @UiThread public void setMessageHandler(@NonNull String channel, @Nullable BinaryMessenger.BinaryMessageHandler handler)
getBinaryMessenger()
instead.BinaryMessenger
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.
setMessageHandler
in interface BinaryMessenger
channel
- the name String
of the channel.handler
- a BinaryMessenger.BinaryMessageHandler
to be invoked on incoming messages, or null.@UiThread public int getPendingChannelResponseCount()
When sending messages to the Flutter application using BinaryMessenger.send(String,
ByteBuffer, io.flutter.plugin.common.BinaryMessenger.BinaryReply)
, developers can optionally
specify a reply callback if they expect a reply from the Flutter application.
This method tracks all the pending callbacks that are waiting for response, and is supposed to be called from the main thread (as other methods). Calling from a different thread could possibly capture an indeterministic internal state, so don't do it.
Currently, it's mainly useful for a testing framework like Espresso to determine whether all the async channel callbacks are handled and the app is idle.
@Nullable public String getIsolateServiceId()
public void setIsolateServiceIdListener(@Nullable io.flutter.embedding.engine.dart.DartExecutor.IsolateServiceIdListener listener)
public void notifyLowMemoryWarning()
This does not notify a Flutter application about memory pressure. For that, use the SystemChannel#sendMemoryPressureWarning
.