UseCase<T, Params> class
abstract
Base UseCase class for Clean Architecture.
A UseCase represents a single business operation. It encapsulates the application's business logic and returns a Result indicating success or failure.
This is the default UseCase type for single-shot operations. For streaming/reactive operations, use StreamUseCase. For operations that don't return a value, use CompletableUseCase.
Key Features
- Returns Result<Type, AppFailure> for type-safe error handling
- Built-in cancellation support via CancelToken
- Automatic error wrapping and logging
- Callable syntax for clean API
Example
class GetUserUseCase extends UseCase<User, String> {
final UserRepository _repository;
GetUserUseCase(this._repository);
@override
Future<User> execute(String userId, CancelToken? cancelToken) async {
return _repository.getUser(userId);
}
}
// Usage
final result = await getUserUseCase('user-123');
result.fold(
(user) => print('Got user: ${user.name}'),
(failure) => print('Error: ${failure.message}'),
);
Error Handling
- Throw AppFailure subclasses for expected errors
- Any other exception is automatically wrapped in UnknownFailure
- CancelledException is converted to CancellationFailure
- Mixed-in types
- Implementers
Constructors
- UseCase()
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- logger → Logger
-
Logger instance for this class.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
Params params, {CancelToken? cancelToken}) → Future< Result< T, AppFailure> > -
Execute the use case with the given
params. -
execute(
Params params, CancelToken? cancelToken) → Future< T> - Override this method to implement the use case logic.
-
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