fl_mirror 1.0.0 copy "fl_mirror: ^1.0.0" to clipboard
fl_mirror: ^1.0.0 copied to clipboard

A robust Dart utility for safe, functional-style handling of success and failure using Mirror<T>, detailed Failure types, and value equality via Mirrorable.

fl_mirror #

A robust Dart utility package for representing and handling operation results, failures, and value equality with ease.


null safety build license: MIT

✨ Features #

  • Mirror<T>: A generic container to represent either a successful result (MirrorSuccess) or a failure (MirrorFailure), enabling functional-style handling of success/failure without exceptions.
  • Failure hierarchy: Rich set of failure types (e.g., ServerFailure, NetworkFailure, UnauthorizedFailure, ...) with severity levels, error codes, hints, and stack traces for detailed error management.
  • Mirrorable base class: Implements value equality based on specified properties (props), with validation to ensure correctness. A lightweight alternative to packages like equatable.
  • πŸ§ͺ Utilities for:
    • Mapping, recovering, and delaying asynchronous or synchronous results.
    • Easily branching logic based on success or failure outcomes.
    • Clean and consistent error classification for server, network, cache, local, and authorization issues.
  • πŸ§ͺ Fully testable.

πŸ› οΈ Installation #

Add this to your pubspec.yaml:

dependencies:
  fl_mirror: ^1.0.0

Then run:

dart pub get

πŸš€ Getting Started #

1. Handling success and failure with Mirror<T> #

  Mirror<int> result = MirrorSuccess(42);

  result.fold(
    onSuccess: (value) => print('Success: $value'),
    onFailure: (error) => print('Failure: $error'),
  );

2. Defining and using failures #

  void handleFailure(Failure failure) {
    print('Error: ${failure.message}');
    if (failure.hint != null) {
      print('Hint: ${failure.hint}');
    }
  }
  
  // Example usage
  final failure = ServerFailure();
  handleFailure(failure);
  // Output:
  // Error: A server error occurred.
  // Hint: Try again later.

3. Creating value objects with Mirrorable #

class Person extends Mirrorable {
  final int id;
  final String name;

  Person(this.id, this.name);

  @override
  List<Object?> get props => [id, name];
}

void main() {
  final a = Person(1, 'Alice');
  final b = Person(1, 'Alice');

  print(a == b); // true
  print(a.hashCode == b.hashCode); // true
  print(a); // Person(1, Alice)
}

πŸ‘οΈ API Overview #

Mirror<T> #

  • MirrorSuccess<T>(value) β€” success container.
  • MirrorFailure<T>(error) β€” failure container.
  • fold(), map(), recover(), delay(), onSuccessDo(), onFailureDo() β€” utilities for functional handling.

Failure and subclasses #

  • Base class with message, code, level, hint, source, stackTrace.
  • Predefined subclasses:
    • ServerFailure
    • UnauthorizedFailure
    • ForbiddenFailure
    • NotFoundFailure
    • LocalFailure
    • CacheFailure
    • NetworkFailure
    • TimeoutFailure

Mirrorable #

  • Abstract base class for value equality.
  • Enforces non-empty and unique props.
  • Provides ==, hashCode, and toString() implementations.

❓ Why use this package ? #

  • Avoid exceptions: Handle success and failure explicitly and safely.
  • Consistent failure handling: Use predefined failure types with detailed info.
  • Simplify equality: Easily implement value objects without boilerplate.
  • Functional style: Fluent API for transforming and recovering from failures.

πŸ“¦ Contributing #

Contributions and suggestions are welcome! Feel free to open issues or pull requests.


πŸ“ Example #

See example/main.dart for a working login use case.

πŸ§ͺ Tests #

Run tests using:

dart test

πŸ“„ License #

This project is licensed under the MIT License.

1
likes
160
points
19
downloads

Publisher

unverified uploader

Weekly Downloads

A robust Dart utility for safe, functional-style handling of success and failure using Mirror<T>, detailed Failure types, and value equality via Mirrorable.

Repository (GitHub)
View/report issues

Topics

#result-type #error-handling #failure #value-object #dart

Documentation

Documentation
API reference

License

MIT (license)

More

Packages that depend on fl_mirror