useTailoredIsolate<Q, R> function

TailoredStatefulIsolate<Q, R> useTailoredIsolate<Q, R>({
  1. BackpressureStrategy<Q, R>? backpressureStrategy,
})

A hook that exposes a TailoredStatefulIsolate.

The hook allows for overriding the default backpressure strategy by setting backpressureStrategy.

This example uses the hook for checking if a number is a prime value on each click of a button.

class TestingIsolateHook extends HookWidget {
  const TestingIsolateHook({super.key});

  @override
  Widget build(BuildContext context) {
    final isolate = useTailoredIsolate<int, bool>();
    final number = useState(1);

    return TextButton(
      onPressed: () async {
        var isPrime = await isolate.compute(_isPrime, number.value);
        print('${number.value} is a prime number? ${isPrime}');
        number.value += 1;
      },
      child: Text(
        'Check if ${number.value} is a prime number',
      ),
    );
  }

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

Implementation

TailoredStatefulIsolate<Q, R> useTailoredIsolate<Q, R>({
  BackpressureStrategy<Q, R>? backpressureStrategy,
}) {
  return use(_IsolateHook<Q, R>(backpressureStrategy));
}