df_safer_dart 0.10.0 copy "df_safer_dart: ^0.10.0" to clipboard
df_safer_dart: ^0.10.0 copied to clipboard

A package inspired by functional programming, designed to enhance the structure, safety, and debuggability of mission-critical code.

example/example.dart

// Example:
//
// Using Resolvable and Result for safer error handling without try-catch
// blocks.
//
// Explicit error handling is enforced, providing compile-time safety.

import 'package:df_safer_dart/df_safer_dart.dart';

import 'dart:convert';
import 'package:http/http.dart' as http;

void main() async {
  // Fetch the IP address and handle both success and error results.
  fetchIpAddress().flatMap(
    (result) => result
        .ifOk((e) {
          print('IP address: ${result.unwrap()}');
        })
        .ifErr((e) {
          print('Error: $e');
        }),
  );
}

Async<String> fetchIpAddress() {
  // Async, Sync or Resolvable can be used to wrap
  // potentially throwing code.
  //
  // The only rules here are:
  //
  // 1. ALWAYS await all asynchronous operations inside Async
  // (or Resolvable) to ensure that exceptions are properly caught and
  // wrapped in a Result.
  //
  // 2. Only deal with asynchronous operations in Async or
  // Resolvable. Not in Sync.
  //
  // 3. You can throw any Objects within unsafe, but prefer throwing Err
  // objects as it is the standard and will help with debugging.
  return Async(() async {
    final response = await http.get(
      Uri.parse('https://api.ipify.org?format=json'),
    );
    // Throw an Err if the status code is not 200. Any other exceptions within
    // Resolvable.wrap will be caught and wrapped in an Err.
    if (response.statusCode != 200) {
      throw Err(
        // The debugPath will be printed when the error is thrown.
        debugPath: ['fetchIpAddress'],
        error: 'Failed to fetch IP address',
      );
    }
    final data = jsonDecode(response.body);
    final ip = data['ip'] as String;
    return ip;
  });
}
copied to clipboard
1
likes
140
points
393
downloads

Publisher

verified publisherdev-cetera.com

Weekly Downloads

2024.09.29 - 2025.04.13

A package inspired by functional programming, designed to enhance the structure, safety, and debuggability of mission-critical code.

Homepage
Repository (GitHub)

Topics

#rust #error-handling #functional-programming #debugging #monads

Documentation

API reference

Funding

Consider supporting this project:

www.buymeacoffee.com

License

MIT (license)

Dependencies

equatable, meta

More

Packages that depend on df_safer_dart