-
-
Notifications
You must be signed in to change notification settings - Fork 7
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.
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 { ... }-
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 forjs | wasm. -
TargetPlatform.all: Shortcut forvm | web.
-
Expose methods to the worker by annotating them with @SquadronMethod().
@SquadronMethod()
Future<int> add(int a, int b) async => a + b;All Squadron methods must be asynchronous in nature, as they cross thread boundaries.
-
Future<T>: Standard async result. -
FutureOr<T>: Can return a value synchronously in the service logic, but it will still be awaited by the caller. -
Stream<T>: For methods that emit multiple values over time.
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 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.
💖 Support the project! Sponsor d-markey on GitHub.