error_or_plus 0.2.0 copy "error_or_plus: ^0.2.0" to clipboard
error_or_plus: ^0.2.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("Luca Fabbri")
      .thenDo((value) => print("${value.name} was created."));
}

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
0
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

License

unknown (license)

More

Packages that depend on error_or_plus