dart_result_option 2.0.1 copy "dart_result_option: ^2.0.1" to clipboard
dart_result_option: ^2.0.1 copied to clipboard

A simple error handling library to help you always treat successes and failures across your code.

Dart Result Option #

Dart Result and Option are example implementations from Rust Option and Result:

Result<int, String> sumPositive(int n1, int n2) {
  if (n1 + n2 < 0) {
    // If the sum is not positive, we return a String wrapped on an Err
    // that will be treated as an error.
    return Err("The sum between $n1 + $n2 is not bigger than 0");
  }

  // If everything goes well we wrap the sum with an Ok.
  return Ok(n1 + n2);
}

sumPositive(5, 5).match(
      (ok) => print("Successfully returned, the sum is $ok"),
      (err) => print(err));
Option<int> testOption(int value) {
  if (value <= 2) {
    return None();
  } else {
    return Some(value * value);
  }
}

testOption(5).match((some) {
  print("value is $some");
}, () {
  print("Value is less than 2");
});

These constructs can be used to handle errors and possible null values, you can look inside example and tests folders for a better idea on how to use them.

For more in-depth explanations go to:

1
likes
140
points
43
downloads

Publisher

unverified uploader

Weekly Downloads

A simple error handling library to help you always treat successes and failures across your code.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on dart_result_option