flutter_resultable 0.0.1
flutter_resultable: ^0.0.1 copied to clipboard
A lightweight Result type for modeling success/failure in Flutter and Dart using sealed classes.
flutter_resultable #
A lightweight, expressive Result
type for Flutter and Dart to model success and failure outcomes without exceptions, inspired by functional programming patterns like Either
.
β¨ Features #
- β
Result.success
andResult.failure
to represent outcomes cleanly. - π Pattern matching via
when
,map
, andmaybeWhen
(thanks tofreezed
). - β‘ Strongly typed error and success handling.
- π‘ Easily testable and composable.
- πͺΆ Minimal, clean API.
π¦ Installation #
Add this to your pubspec.yaml
:
dependencies:
flutter_resultable: ^1.0.0
Then run:
flutter pub get
π§ Usage #
Define a result-returning function #
import 'package:flutter_resultable/flutter_resultable.dart';
Result<String, int> divide(int a, int b) {
if (b == 0) return Result.failure("Division by zero");
return Result.success(a ~/ b);
}
Handle with pattern matching #
final result = divide(10, 2);
result.when(
failure: (error) => print("Error: $error"),
success: (value) => print("Result: $value"),
);
Using maybeWhen
#
result.maybeWhen(
success: (value) => print("Success with $value"),
orElse: () => print("Something went wrong"),
);
π API #
sealed class Result<F, S> {
const factory Result.failure(F failure) = Failure<F, S>;
const factory Result.success(S success) = Success<F, S>;
}
Result.failure(F)
β represents a failure result.Result.success(S)
β represents a successful result.
π οΈ Generated Code #
This package uses freezed. To generate code locally (for contributors):
flutter pub run build_runner build --delete-conflicting-outputs
π License #
MIT License
π° Support #
If you find this project helpful, consider sponsoring me on GitHub π
π Contributing #
Contributions, ideas, and pull requests are welcome. Letβs make flutter_resultable
better together!