light_result library
A lightweight functional error handling library for Dart 3.
The core library (package:light_result/light_result.dart) has zero dependencies.
Optional test matchers are available via package:light_result/testing.dart
(depends on matcher).
Core Types
- Result — Represents either a success (Success) or failure (Failure).
- Option — Represents an optional value (Some) or absence (None).
Quick Start
import 'package:light_result/light_result.dart';
// Creating results
final success = Success<String, int>(42);
final failure = Failure<String, int>('Something went wrong');
// Pattern matching (exhaustive)
final message = switch (success) {
Failure(value: final error) => 'Error: $error',
Success(value: final data) => 'Data: $data',
};
// Functional chaining
final result = Success<String, int>(10)
.map((x) => x * 2)
.flatMap((x) => x > 0 ? Success(x) : Failure('negative'));
Classes
- AppError
- Base failure class for structured error handling.
-
Failure<
L, R> -
Represents a failed computation containing a value of type
L. -
None<
T> - Represents the absence of a value.
-
Option<
T> - Functional Option type — a safe alternative to nullable types.
-
Result<
L, R> - Core Result monad — the heart of functional error handling.
-
Some<
T> - Represents the presence of a value.
-
Success<
L, R> -
Represents a successful computation containing a value of type
R. - Unit
- Represents the absence of a meaningful return value in a Result.
Extensions
-
TaskOption
on Future<
Option< T> > -
Extensions on
Future<Option<T>>for fluent async option chaining. -
TaskResult
on Future<
Result< L, R> > -
Extensions on
Future<Result<L, R>>to enable fluent async chaining.
Typedefs
-
AppResult<
T> = Result< AppError, T> - A Result where the failure type is a custom AppError class.
-
AsyncResult<
L, R> = Future< Result< L, R> > - A Future returning a Result.
-
AsyncResultOf<
T> = Future< Result< Exception, T> > - A Future returning a ResultOf (failure type = Exception).
-
Either<
L, R> = Result< L, R> - Type alias for Result — for developers migrating from fpdart/dartz.
-
Left<
L, R> = Failure< L, R> - Deprecated: Use Failure instead.
-
ResultOf<
T> = Result< Exception, T> - A Result where the failure type is Exception.
-
Right<
L, R> = Success< L, R> - Deprecated: Use Success instead.
-
StringResult<
T> = Result< String, T> - A Result where the failure type is String.