Worker class

An abstraction of the Isolate to make it easier to use without loosing the control of it's capabilities.

Each worker will handle one isolate (thread), before using it you need to call the init function providing the parameters that you need. Since the isolate needs to be started and return the port to communicate with the main thread, it needs to be awaited.

Example:


...
Future myMethod() async {
  final worker = Worker();
  await worker.init(mainMessageHandler, isolateMessageHandler);
}

void mainMessageHandler(dynamic data) {}

static void isolateMessageHandler(
  dynamic data, SendPort mainSendPort, SendErrorFunction sendError) {}
...

Note: The isolate handler should be static or a top level function, and since it's called inside another thread doesn't share any instance with the main thread.

Check the init and send documentation for more information.

Constructors

Worker()

Properties

hashCode int
The hash code for this object.
no setterinherited
isInitialized bool
Return if the worker is initialized. Can be used to validate before sending messages in the case where it's not possible to await the init execution.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

dispose({bool immediate = false}) → void
Responsible for killing the isolate and close the main thread port
init(MainMessageHandler mainHandler, IsolateMessageHandler isolateHandler, {Object? initialMessage = const _NoParameterProvided(), bool queueMode = false, MessageHandler? errorHandler, MessageHandler? exitHandler}) Future<void>
The worker initializer.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
sendMessage(Object? message) → void
Responsible for sending messages to the isolate.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited