BackgroundUseCase<T, Params> class abstract

A UseCase that executes expensive operations on a separate isolate.

Use BackgroundUseCase for CPU-intensive operations that would block the main UI thread, such as:

  • Image processing
  • Large data parsing
  • Complex calculations
  • Encryption/decryption

Key Features

  • Runs on a separate isolate to avoid UI jank
  • Streams results back via Result<Type, AppFailure>
  • Proper resource cleanup
  • Built-in cancellation support

Important Constraints

  • The buildTask must return a static or top-level function
  • Parameters must be serializable (primitives, Lists, Maps)
  • Not supported on web (will throw assertion error)

Example

class MatrixMultiplyUseCase extends BackgroundUseCase<Matrix, MatrixParams> {
  @override
  BackgroundTask<MatrixParams> buildTask() => _multiply;

  // MUST be static or top-level
  static void _multiply(BackgroundTaskContext<MatrixParams> context) {
    final params = context.params;
    final result = params.matrixA * params.matrixB;
    context.sendData(result);
    context.sendDone();
  }
}

// Usage
matrixMultiplyUseCase(params).listen((result) {
  result.fold(
    (matrix) => print('Result: $matrix'),
    (failure) => print('Error: ${failure.message}'),
  );
});
Mixed-in types

Constructors

BackgroundUseCase()
Create a new BackgroundUseCase.

Properties

hashCode int
The hash code for this object.
no setterinherited
isRunning bool
Whether the use case is currently running
no setter
logger → Logger
Logger instance for this class.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
state BackgroundUseCaseState
Current execution state
no setter

Methods

buildTask() BackgroundTask<Params>
Override this method to provide the background task.
call(Params params, {CancelToken? cancelToken}) Stream<Result<T, AppFailure>>
Execute the use case with the given params.
dispose() → void
Dispose of all resources.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited