squadron 6.0.0 copy "squadron: ^6.0.0" to clipboard
squadron: ^6.0.0 copied to clipboard

Multithreading and worker thread pool for Dart / Flutter, to offload CPU-bound and heavy I/O tasks to Isolate or Web Worker threads.

Squadron logo

Squadron - Multithreading and worker pools in Dart #

Offload CPU-bound and long running tasks and give your apps some air!

Works everywhere: desktop, server, device, browser.

Supports native, JavaScript & Web Assembly platforms.

Pub Package Dart Platforms Flutter Platforms

License Null Safety Dart Style Pub Points Likes Popularity

Last Commit Dart Workflow Code Lines Code Coverage

Getting Started #

  1. Update your pubspec.yaml file to add dependencies to squadron and squadron_builder:
dependencies:
  squadron: ^6.0.0
  # ...
  # other dependencies used by your app
  # ...

dev_dependencies:
  build_runner:
  squadron_builder: ^6.0.0
  # ...
  # other dev dependencies used by your app
  # ...
  1. Have dart download and install the dependencies:
dart pub get

Implementing a Service #

Create a class containing the code you want to run in a dedicated thread and make sure you provide squadron annotations:

  • use SquadronService for the class;
  • use SquadronMethod for the methods you want to expose.

Service methods must return a Future<T>, a FutureOr<T> or a Stream<T>.

// file hello_world.dart
import 'dart:async';

import 'package:squadron/squadron.dart';

import 'hello_world.activator.g.dart';
part 'hello_world.worker.g.dart';

@SquadronService(baseUrl: '~/workers', targetPlatform: TargetPlatform.vm | TargetPlatform.web)
base class HelloWorld {
  @SquadronMethod()
  FutureOr<String> hello([String? name]) {
    name = name?.trim() ?? 'World';
    return 'Hello, $name!';
  }
}

Generate the Worker and WorkerPool code #

Have squadron_builder generate the code with the following command line:

dart run build_runner build

This command will create the worker and worker pool from your service: HelloWorldWorker and HelloWorldWorkerPool.

Run your code #

// file main.dart
import 'package:squadron/squadron.dart';

import 'hello_world.dart';

void main() async {
  final worker = HelloWorldWorker();
  try {
    // Squadron will start the worker for you so you don't have to call worker.start()
    final message = await worker.hello();
    print(message);
  } finally {
    // make sure the worker is stopped when the program terminates
    worker.stop();
  }
}

Building for the Web #

If your app runs in a browser, you must compile your code to JavaScript or Wasm.

dart compile js .\hello_world.web.g.dart -o ..\web\workers\hello_world.web.g.dart.js
dart compile wasm .\hello_world.web.g.dart -o ..\web\workers\hello_world.web.g.dart.wasm

Thanks! #

  • Saad Ardati for his patience and feedback when implementing Squadron into his Flutter application.
  • Martin Fink for the feedback on Squadron's Stream implementation -- this has resulted in huge progress and a major improvement!
  • Klemen Tusar for providing a sample Chopper JSON decoder leveraging Squadron.
159
likes
0
pub points
89%
popularity

Publisher

unverified uploader

Multithreading and worker thread pool for Dart / Flutter, to offload CPU-bound and heavy I/O tasks to Isolate or Web Worker threads.

Repository (GitHub)
View/report issues

Topics

#concurrency #isolate #multithread #web-worker

Funding

Consider supporting this project:

github.com

License

unknown (license)

Dependencies

cancelation_token, logger, meta, using, web

More

Packages that depend on squadron