resilify 1.0.6 copy "resilify: ^1.0.6" to clipboard
resilify: ^1.0.6 copied to clipboard

Unified Result-based API handling for Dart & Flutter — works with http, Dio, Retrofit, Chopper, and WebSocket. No exceptions. Just results.

example/resilify.dart

// Quick tour of the core `resilify` API — no networking dependencies.
//
// Run with: `dart run example/resilify.dart`
//
// For framework-specific integrations, see the sibling examples:
//   - http_example.dart        (package:http)
//   - dio_example.dart         (package:dio)
//   - retrofit_example.dart    (package:retrofit)
//   - chopper_example.dart     (package:chopper)
//   - websocket_example.dart   (package:web_socket_channel)
//   - retry_example.dart       (RetryHelper with exponential backoff)

import 'package:resilify/resilify.dart';

Future<void> main() async {
  // 1. Build Results directly.
  const ok = Success<int>(42);
  const bad = Error<int>(Failure.notFound());

  // 2. Pattern-match exhaustively.
  print(ok.when(success: (v) => 'got $v', error: (f) => 'oops: ${f.message}'));
  print(bad.when(success: (v) => 'got $v', error: (f) => 'oops: ${f.message}'));

  // 3. Transform success values; failures short-circuit automatically.
  final doubled = ok.map((v) => v * 2);
  print(doubled); // Success<int>(84)

  // 4. Translate low-level failures into domain failures.
  final domain = bad.mapError(
    (f) => Failure.unknown(message: 'lookup failed: ${f.message}'),
  );
  print(domain.errorOrNull?.message);

  // 5. Bridge throwing code into a Result without writing try/catch.
  final parsed = Result.tryRun<int>(() => int.parse('not a number'));
  print(parsed.isError); // true

  // 6. Same idea, but async.
  final fetched = await Result.tryRunAsync<String>(() async {
    await Future<void>.delayed(const Duration(milliseconds: 10));
    return 'hello';
  });
  print(fetched.getOrElse('fallback'));

  // 7. Recover from a failed future without throwing.
  final recovered = await Future.value(bad).recover((f) async => -1);
  print(recovered); // Success<int>(-1)
}
1
likes
0
points
522
downloads

Publisher

verified publisherhimanshulahoti.is-a.dev

Weekly Downloads

Unified Result-based API handling for Dart & Flutter — works with http, Dio, Retrofit, Chopper, and WebSocket. No exceptions. Just results.

Repository (GitHub)
View/report issues

Topics

#result #error-handling #dio #http #websocket

License

unknown (license)

Dependencies

chopper, dio, http, retrofit, web_socket_channel

More

Packages that depend on resilify