fabrik_result
fabrik_result is a minimal result-handling toolkit for Dart and Flutter. It gives you a typed way to model success and failure without relying on exceptions — making your domain logic easier to read, test, and reason about.
Installation
Section titled “Installation”dart pub add fabrik_resultOr add it to your pubspec.yaml:
dependencies: fabrik_result: ^1.0.1Import it in your Dart files:
import 'package:fabrik_result/fabrik_result.dart';Either
Section titled “Either”Either<L, R> is a sealed type that holds either a left value (conventionally failure) or a right value (conventionally success). It makes the possible outcomes of an operation explicit in the return type.
Either<Failure, User> result = await userRepository.getUser(id);
result.fold( (failure) => showError(failure.message), (user) => navigateToProfile(user),);Constructing values:
// Successreturn Right(user);
// Failurereturn Left(Failure('User not found'));Checking the side:
result.isRight // true if successresult.isLeft // true if failureUnit is a typed void — use it when an operation succeeds but has nothing meaningful to return.
Either<Failure, Unit> result = await settingsRepository.save(settings);
result.fold( (failure) => showError(failure.message), (_) => showSuccess('Settings saved'),);Instead of returning Either<Failure, void> (which isn’t valid in Dart), return Either<Failure, Unit> and use Right(unit) as the success value.
In a Repository
Section titled “In a Repository”A typical repository method using fabrik_result:
Future<Either<Failure, User>> getUser(String id) async { try { final model = await dataSource.fetchUser(id); return Right(model.toEntity()); } catch (e) { return Left(Failure(e.toString())); }}Why Not Try-Catch?
Section titled “Why Not Try-Catch?”try-catch | Either | |
|---|---|---|
| Failure is explicit in signature | No | Yes |
| Caller must handle failure | No | Yes |
| Testable without exceptions | Hard | Easy |
| Composable | No | Yes |
fabrik_result is a lightweight alternative to packages like dartz — it provides only Either and Unit, nothing more.