flutter_heyteacher_worker 2.0.6+138 copy "flutter_heyteacher_worker: ^2.0.6+138" to clipboard
flutter_heyteacher_worker: ^2.0.6+138 copied to clipboard

The flutter heyteacher package for worker utilities.

Flutter Heyteacher Worker #

This package provides a generics Worker<I,O> class to run long-running tasks in a background isolate, preventing the UI from freezing. It simplifies the process of offloading computation from the main thread.

The implementation is based on Robust ports example described in Dart Isolates official documentation.

Features #

  • Worker<I, O>: A generics class that manages the lifecycle of a long-running isolate.

Important

The Worker<I, O> will not spawn a real isolate on the web plaftorm or during Flutter tests.

In web and Flutter tests environments, the task will be executed on the main thread instead.

Usage #

Import the main library file to access the components:

import 'package:flutter_heyteacher_worker/worker.dart';

To use Worker<I, O>, you need to:

  • define a top-level or static function that matches the signature Future<O> function(I input)
  • pass it to the Worker constructor.

Example #

Here is a simple example of how to use the Worker<I, O> to perform a computation in a background isolate.

// 1. Define your worker logic as a top-level or static function.
// This function will be executed in the background isolate.
// `I` is the input type is `String` 
// `O` is the output type is `int`.
Future<int> calculateStringLength(String input) async {
  // Simulate a heavy computation
  await Future.delayed(const Duration(seconds: 1));
  return input.length;
}

Future<void> runWorker() async {
  final worker = Worker<String, int>(calculateStringLength);
  final result = await worker.execute('hello worker');

  print('Result from worker: ${result.output}'); // Result from worker: 12

  worker.close();
}
0
likes
150
points
2
downloads

Documentation

Documentation
API reference

Publisher

unverified uploader

Weekly Downloads

The flutter heyteacher package for worker utilities.

Repository
View/report issues

Topics

#flutter #worker #isolate

Funding

Consider supporting this project:

liberapay.com

License

BSD-3-Clause (license)

Dependencies

clock, flutter, logging

More

Packages that depend on flutter_heyteacher_worker