cancelable_compute 1.1.1 cancelable_compute: ^1.1.1 copied to clipboard
Spawn an isolate, run callback on that isolate, passing it message, and return the value returned by callback or canceled by user.
import 'package:cancelable_compute/cancelable_compute.dart';
Future<void> main() async {
final operation = compute(fib, 256);
Future<void>.delayed(Duration(seconds: 1), operation.cancel);
final result = await operation.value;
print(result ?? -1);
}
int fib(int n) {
if (n < 2) {
return n;
}
return fib(n - 2) + fib(n - 1);
}