either_type 0.1.2
either_type: ^0.1.2

Dart native js
Flutter Android iOS web

The library for Error Handling. Armed with Either, you can write code that returns either an exception or a legitimate result (but never both) while retaining type safety. The common functional conven

Either #

The library for Error Handling.

main() async {
  // There is some method that will return the user or error.
  await getUser()
    ..either((ServerError error) {
      // Add a handler for the error.
      print("Error: ${error.code}");
    }, (User user) {
      // Add a handler to get the user.
      print("User: ${user.name}");
    });

  // or
  final either = await getUser();
  if (either.isLeft) {
    final error = either.left;
    print("Error: ${error.code}");
  } else {
    final user = either.right;
    print("User: ${user.name}");
  }
}

// And the return of either looks like this.
// According to the agreement, the right part stores the result of success,
// left is wrong.
Future<Either<ServerError, User>> getUser() async =>
    (Random().nextBool()) ? Right(User("Bob")) : Left(ServerError(500));

Installation #

Add on pubspec.yml:

dependencies:
  either_type: ^0.1.2

Getting Started #

This project is a starting point for a Dart package, a library module containing code that can be shared easily across multiple Flutter or Dart projects.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

1
likes
80
pub points
64%
popularity

The library for Error Handling. Armed with Either, you can write code that returns either an exception or a legitimate result (but never both) while retaining type safety. The common functional convention is that the left of an Either class contains an exception (if any), and the right contains the result.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

baihu92@gmail.com

License

MIT (LICENSE)

More

Packages that depend on either_type