TailoredStatefulIsolate<Q, R> class final

A class that makes running code in an isolate almost as easy as running Flutter's compute function.

This is a tailored version of StatefulIsolate, where you can specify what input type and output type is allowed.

  • The generic type Q is used for input type.
  • The generic type R is used for output type.

Using backpressureStrategy, you can decide how to handle the case when too many calls to the isolate are made for it to handle in time.

The compute function is used the same way as the compute function in the Flutter library.

The following code is similar to the example from Flutter's compute function, but allows for reusing the same isolate for multiple calls.

void main() async {
  final statefulIsolate = TailoredStatefulIsolate<int, bool>();
  print(await statefulIsolate.compute(_isPrime, 7));
  print(await statefulIsolate.compute(_isPrime, 42));
  print(await statefulIsolate.compute(_isPrime, 50));
  print(await statefulIsolate.compute(_isPrime, 70));
  statefulIsolate.dispose();
}

bool _isPrime(int value) {
  if (value == 1) {
    return false;
  }
  for (int i = 2; i < value; ++i) {
    if (value % i == 0) {
      return false;
    }
  }
  return true;
}

See also:

  • StatefulIsolate, to create a long lived isolate that has no predefined input and output typed, but rather decides type per call to the compute function.
Implemented types

Constructors

TailoredStatefulIsolate({BackpressureStrategy<Q, R>? backpressureStrategy, bool autoInit = true})
Creates a minimal isolate that requires a specific input type Q, and that can only be used with functions returning type R

Properties

backpressureStrategy BackpressureStrategy<Q, R>
Implementations of StatefulIsolate has to override this to specify a backpressureStrategy.
final
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

addIsolateCall(BackpressureConfiguration<Q, R> createBackpressureConfiguration(Flow flow)) → void
Internal helper function to wrap creation of an isolate call, add it to queue and start running the queue.
inherited
compute(IsolateCallback<Q, R> callback, Q message, {String? debugLabel}) Future<R>
The computation function, a function used the same way as Flutter's compute function, but for a long lived isolate.
override
computeStream(IsolateStream<Q, R> callback, Q message, {String? debugLabel}) Stream<R>
A computation function that returns a Stream of responses from the long lived isolate.
override
dispose() Future
Closes down the isolate and cancels all jobs currently in queue.
inherited
handleIsolateCall() Future
inherited
init() Future
Initializes the isolate for use.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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