fluent_result 8.5.0 copy "fluent_result: ^8.5.0" to clipboard
fluent_result: ^8.5.0 copied to clipboard

A fluent Result type for Dart that returns success/failure instead of throwing, with a global observability seam for crash reporting.

example/example.dart

import 'package:fluent_result/fluent_result.dart';

void main() {
  // 1. Configure global observability once (e.g. wire Sentry here).
  ResultConfig.onException = (error, stack) {
    // ignore: avoid_print
    print('Reported unexpected: $error');
  };
  ResultConfig.matchers = [
    // Expected control flow - fail quietly, do NOT report.
    ResultMatcher(
      (e) => e is FormatException,
      (e, st) => fail(e),
      expected: true,
    ),
  ];

  // 2. Wrap a throwing computation; never rethrows.
  final parsed = ResultOf.guard<int>(() => int.parse('not-a-number'));
  final message = parsed.match(
    onFail: (errors) => 'parse failed: ${errors.first.message}',
    onSuccess: (value) => 'parsed $value',
  );
  // ignore: avoid_print
  print(message);

  // 3. Validation never reaches onException.
  final v = Result.failIf(() => message.isEmpty, 'message required');
  // ignore: avoid_print
  print(v.isFail ? v.errorMessage : 'ok');
}
16
likes
160
points
763
downloads

Documentation

API reference

Publisher

verified publisherdevcraft.ninja

Weekly Downloads

A fluent Result type for Dart that returns success/failure instead of throwing, with a global observability seam for crash reporting.

Repository (GitHub)
View/report issues

Topics

#result #error-handling #functional #either

License

MIT (license)

Dependencies

collection

More

Packages that depend on fluent_result