Skip to main content

SyncUseCase

SyncUseCase is a specialized base class in Zuraffa designed for business logic that executes immediately on the main thread without any asynchronous I/O. It provides the same type-safe Result wrapping as standard UseCases but without the overhead of Future or Stream.


🦄 Why SyncUseCase?

  • Immediate Execution: No await required. Ideal for UI-blocking validations or transformations.
  • Zero Async Overhead: Avoids the performance cost of the event loop for simple logic.
  • Type Safe: Returns a Result<T, AppFailure>, ensuring consistent error handling across your app.
  • Pure Logic: Perfect for keeping your business rules decoupled from Flutter and infrastructure.

🚀 Basic Usage

1. Generate a Sync UseCase

Use the --type=sync flag with zfa make:

zfa make ValidateEmail usecase \
--domain=validation \
--type=sync \
--params=String \
--returns=bool

2. Implementation

Zuraffa generates a class with a synchronous execute method:

// lib/src/domain/usecases/validation/validate_email_usecase.dart

class ValidateEmailUseCase extends SyncUseCase<bool, String> {

bool execute(String email) {
return RegExp(r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$').hasMatch(email);
}
}

3. Usage in a Controller

Since it's synchronous, you can call it directly within your state updates:

void onEmailChanged(String email) {
final result = _validateEmail(email);

result.fold(
(isValid) => updateState(viewState.copyWith(isValid: isValid)),
(failure) => updateState(viewState.copyWith(error: failure.message)),
);
}

🛠️ Best Use Cases

CategoryExamples
ValidationEmail/Phone regex, password strength, form field checks.
TransformationsMapping API models to UI models, filtering lists, sorting.
CalculationsCart totals, tax math, currency formatting, unit conversion.
Business RulesPermission checks (if local), feature flag evaluation.

🧠 Comparison: UseCase Types

TypeReturn TypeBest For
UseCaseFuture<Result>API calls, Database, File I/O.
SyncUseCaseResultValidations, Math, Transformations.
StreamUseCaseStream<Result>WebSockets, Firebase, DB Watchers.
BackgroundUseCaseFuture<Result>Heavy CPU work (Image processing, Big JSON).

📂 Next Steps