error_or_plus 1.1.0 copy "error_or_plus: ^1.1.0" to clipboard
error_or_plus: ^1.1.0 copied to clipboard

A simple, fluent discriminated union of an error or a result. Inspired by ErrorOr for .net by Amantiband

example/main.dart

import 'package:error_or_plus/error_or_plus.dart';

main() {
  User.create("test").alsoDo((value) => print("${value.name} was created."));

  var boolResult = User.create("test")
      .also<bool>((_) => true.toErrorOr())
      .orElse(valueOnError: false)
      .value;
}

class User {
  final String _name;

  String get name => _name;

  User._internal(this._name);

  static ErrorOr<User> create(String name) {
    List<Errors> errors = [];

    if (name.length < 2) {
      errors.add(Errors.validation(description: "Name is too short"));
    }

    if (name.length > 100) {
      errors.add(Errors.validation(description: "Name is too long"));
    }

    if (name.isEmpty) {
      errors.add(Errors.validation(
          description: "Name cannot be empty or whitespace only"));
    }

    if (errors.isNotEmpty) {
      return errors.toErrorOr<User>();
    }

    return User._internal(name).toErrorOr();
  }
}
4
likes
160
pub points
34%
popularity

Publisher

verified publishernepterius.it

A simple, fluent discriminated union of an error or a result. Inspired by ErrorOr for .net by Amantiband

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

More

Packages that depend on error_or_plus