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

A package inspired by Rust and functional programming to bring more structure and clarity to your Dart applications.

example/example.dart

// Example of avoiding try-catch blocks in Dart, to produce safer code:

import 'package:df_safer_dart/df_safer_dart.dart';

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

void main() async {
  fetchIpAddress().fold(
    (value) => print('IP address: $value'),
    (error) => print('Error: $error'),
  );
}

Result<String> fetchIpAddress() {
  return Result(() async {
    final response =
        await http.get(Uri.parse('https://api.ipify.org?format=json'));
    // This throws an exception if the status code is not 200.
    PanicIf(response.statusCode != 200, 'Failed to fetch IP address');
    final data = jsonDecode(response.body);
    final ip = data['ip'] as String;
    // Return the result.
    return ip;
  });
}
10
likes
0
points
6.57k
downloads

Publisher

verified publisherdev-cetera.com

Weekly Downloads

A package inspired by Rust and functional programming to bring more structure and clarity to your Dart applications.

Repository (GitHub)
View/report issues

Topics

#rust #error-handling #functional-programming

Funding

Consider supporting this project:

www.buymeacoffee.com

License

unknown (license)

Dependencies

meta

More

Packages that depend on df_safer_dart