spawn<T> method

HandledIsolate spawn<T>(
  1. void function(
    1. Map<String, dynamic>
    ), {
  2. String? name,
  3. void onReceive(
    1. T message
    )?,
  4. void onInitialized()?,
  5. bool paused = false,
  6. bool? errorsAreFatal,
  7. SendPort? onExit,
  8. SendPort? onError,
  9. String? debugName,
})

Spawns a new HandledIsolate of type T (dynamic by default) with an entry point of function. Note that because of the limitations of the underlying messaging mechanism of SendPort.send(), the list of allowed types is limited: "The content of message can be: primitive values (null, num, bool, double, String), instances of SendPort, and lists and maps whose elements are any of these. List and maps are also allowed to be cyclic."

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.

function is called by the isolate immediately upon creation, before communication channels have been fully established.

If the onReceive function parameter is provided, it will be called every time a new message has been received from the spawned isolate. The function should be able to be called with a single argument, which will contain data of type T received from the isolate.

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.

The name parameter specifies a unique name by which the isolate can be recalled later from the handler. If no name has been given to an isolate it will receive a generated name with a prefix of __anonymous_ followed by a number. Avoiding names using the same format is good practice. Must be unique.

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

Throws if name is not unique.

Returns spawned HandledIsolate instance.

Implementation

@pragma('vm:entry-point')
HandledIsolate spawn<T>(
  void Function(Map<String, dynamic>) function, {
  String? name,
  void Function(T message)? onReceive,
  void Function()? onInitialized,
  bool paused = false,
  bool? errorsAreFatal,
  SendPort? onExit,
  SendPort? onError,
  String? debugName,
}) {
  assert(name == null || !isolates.containsKey(name));

  name ??= '__anonymous_${_uid++}';
  isolates[name] = HandledIsolate<T>(
      name: name, function: function, onInitialized: onInitialized);
  isolates[name]!
      .messenger
      .listen((dynamic message) => onReceive?.call(message));
  return isolates[name]!;
}