flutter_resultable 0.0.2 copy "flutter_resultable: ^0.0.2" to clipboard
flutter_resultable: ^0.0.2 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 using exceptions, inspired by functional programming patterns like Either.

Pub Version GitHub Actions Workflow Status GitHub License


✨ Features #

  • βœ… Result.success(S) and Result.failure(F) to represent outcomes cleanly.
  • πŸ” Pattern matching via when, map, and maybeWhen (thanks to freezed).
  • ⚑ Strongly typed failure and success handling.
  • 🧠 Utility extensions:
    • isSuccess, isFailure
    • successOrNull, failureOrNull
    • getOrElse(fallback)
    • fold(onFailure, onSuccess)
  • πŸ’‘ Easily testable and composable.
  • πŸͺΆ Minimal, clean API.

πŸ“¦ Installation #

Add this to your pubspec.yaml:

dependencies:
  flutter_resultable: ^0.0.2

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"),
);

Use maybeWhen for optional handling #

result.maybeWhen(
  success: (value) => print("Success with $value"),
  orElse: () => print("Something went wrong"),
);

Use fold for cleaner branching #

result.fold(
  (error) => print("Error: $error"),
  (value) => print("Value: $value"),
);

Use getOrElse to provide a fallback value #

final value = result.getOrElse((error) => -1); // returns 5 or -1

Check result state or access nullable values #

if (result.isSuccess) {
  print(result.successOrNull);
} else {
  print(result.failureOrNull);
}

πŸ“˜ API #

sealed class Result<F, S> {
  const factory Result.failure(F failure) = Failure<F, S>;
  const factory Result.success(S success) = Success<F, S>;
}

Utility Extensions #

Extension Description
isSuccess Returns true if the result is success
isFailure Returns true if the result is failure
successOrNull Returns success value or null
failureOrNull Returns failure value or null
getOrElse Fallback mechanism based on failure value
fold Branching on both success and failure cases

πŸ’° 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!


πŸ™ Thanks #

This package is powered by the amazing freezed package. Special thanks to Remi Rousselet and contributors for their work.

Without freezed, clean and expressive sealed classes like Result wouldn't be this simple ✨


πŸ“„ License #

MIT License


3
likes
140
points
44
downloads

Publisher

unverified uploader

Weekly Downloads

A lightweight Result type for modeling success/failure in Flutter and Dart using sealed classes.

Repository (GitHub)
View/report issues

Topics

#result #either #functional #error-handling #flutter

Documentation

API reference

Funding

Consider supporting this project:

github.com

License

MIT (license)

Dependencies

freezed_annotation

More

Packages that depend on flutter_resultable