HandledIsolate<T> constructor
Create a new instance of HandledIsolate.
Spawns a new HandledIsolate with an entry point of function
.
The argument function
specifies the initial function to call in the
spawned isolate. The entry-point function is invoked in the new isolate
with context
as the only argument.
The function must be a top-level function or a static method that can be called with a single argument, that is, a compile-time constant function value which accepts at least one positional parameter and has at most one required positional parameter.
The function may accept any number of optional parameters, as long as it can be called with just a single argument. The function must not be the value of a function expression or an instance method tear-off.
name
is used by IsolateHandler to identify the isolate. It can be any
String
, but must not be null.
function
is called by the isolate immediately upon creation, before
communication channels have been fully established.
If the function argument onInitialized
is specified, it will be called
once communication channels have been established, meaning that the
HandledIsolate instance is ready to send and receive data.
If the paused
parameter is set to true
, the isolate will start up in
a paused state, just before calling the function
function with the
context
, as if by an initial call of
isolate.pause(isolate.pauseCapability)
. To resume the isolate,
call isolate.resume(isolate.pauseCapability)
.
If the errorsAreFatal
, onExit
and/or onError
parameters are
provided, the isolate will act as if, respectively, setErrorsFatal
,
addOnExitListener
and addErrorListener
were called with the
corresponding parameter and was processed before the isolate starts
running.
If debugName
is provided, the spawned Isolate will be identifiable by
this name in debuggers and logging.
If errorsAreFatal
is omitted, the platform may choose a default behavior
or inherit the current isolate's behavior.
You can also call the setErrorsFatal
, addOnExitListener
and
addErrorListener
methods directly by accessing isolate
, but unless the
isolate was started as paused
, it may already have terminated before
those methods can complete.
Isolates need to be disposed of using kill
when done using them.
Returns spawned HandledIsolate instance.
Implementation
HandledIsolate({
required String name,
required void Function(Map<String, dynamic>) function,
void Function()? onInitialized,
bool paused = false,
bool? errorsAreFatal,
SendPort? onExit,
SendPort? onError,
String? debugName,
}) : _messenger = HandledIsolateMessenger(onInitialized: onInitialized),
_name = name {
_init(
function,
paused: paused,
errorsAreFatal: errorsAreFatal,
onExit: onExit,
onError: onError,
debugName: debugName,
);
}