use_isolate 0.2.0+2 icon indicating copy to clipboard operation
use_isolate: ^0.2.0+2 copied to clipboard

A Hook wrapper of integral_isolates making multi-threading easy using hooks

The power of integral_isolates neatly packed up in a hook.

Usage #

Using an isolate in a hook has never been simpler. With the use of useIsolate() we you can get a compute function similar to compute but that lives longer. You don't have to care about lifecycle, the hook handles that for you.

Example:

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

  @override
  Widget build(BuildContext context) {
    final isolate = useIsolate();
    final number = useState(1);

    return TextButton(
      onPressed: () async {
        var isPrime = await isolate(_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;
  }
}

Just as integral_isolates, this hook supports backpressure strategies, just send a strategy in as parameter:

useIsolate(backpressureStrategy: DiscardNewBackPressureStrategy());

Additional information #

You could expect this API to be mostly stable, but implementation of the underlying package (integral_isolates) is not fully finalized yet, and there is more features coming before both packages can count as stable.

11
likes
120
pub points
30%
popularity

Publisher

verified publisher iconlohnn.se

A Hook wrapper of integral_isolates making multi-threading easy using hooks

Repository (GitHub)
View/report issues

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

flutter, flutter_hooks, integral_isolates

More

Packages that depend on use_isolate