Skip to content

Defining Services

d-markey edited this page Jan 22, 2026 · 4 revisions

A Service in Squadron is where you define the business logic that needs to run in a separate thread.


The @SquadronService Annotation

Every service class must be annotated with @SquadronService.

@SquadronService(
  baseUrl: '~/workers',         // Path to find worker scripts (mostly for Web)
  targetPlatform: TargetPlatform.all // .vm, .js, .wasm, .web (js|wasm), or .all
)
base class MyService { ... }

Parameters

  • baseUrl: Essential for Web platforms. It tells the browser where to find the generated worker entry point files. Usually relative to the deployment root.
  • targetPlatform: Specifies which platforms this service supports.
    • TargetPlatform.vm: Native platforms (Mobile, Desktop, Server).
    • TargetPlatform.js: Web (JavaScript).
    • TargetPlatform.wasm: Web (Web Assembly).
    • TargetPlatform.web: Shortcut for js | wasm.
    • TargetPlatform.all: Shortcut for vm | web.

The @SquadronMethod Annotation

Expose methods to the worker by annotating them with @SquadronMethod().

@SquadronMethod()
Future<int> add(int a, int b) async => a + b;

Supported Return Types

All Squadron methods must be asynchronous in nature, as they cross thread boundaries.

  1. Future<T>: Standard async result.
  2. FutureOr<T>: Can return a value synchronously in the service logic, but it will still be awaited by the caller.
  3. Stream<T>: For methods that emit multiple values over time.

Arguments

Arguments must be:

  • Primitive types (int, double, bool, String).
  • Standard collections (List, Map, Set) containing primitives.
  • TypedData (e.g., Uint8List).
  • Custom objects that are transferable or have a registered Marshaler (see Marshaling).

Private Members

Private methods and fields are not exposed to the generated Worker. You can use them for internal logic within the service thread.

Next Step: Learn more about Annotations.

Clone this wiki locally