dartplus 0.0.2 copy "dartplus: ^0.0.2" to clipboard
dartplus: ^0.0.2 copied to clipboard

Simple & Lightweight Error Handling Solution | Influenced by Either in Functional Programming Languages

example/main.dart

import 'package:dartplus/dartplus.dart';

main() async {
  final result = await login("vicky");

  if (result.isLeft)
    print("Failure : ${result.left?.error}");
  else
    print("Success : Logged in as ${result.right?.name}");
}

Future<Either<AppError, User>> login(String username) async {
  await Future.delayed(Duration(seconds: 2));
  if (username.length < 6) return left(AppError(error: "Invalid Username!"));
  return right(User(name: username));

  // You can implement a try/catch bloc with real login
  // If login success return right(User)
  // If login fails return left(AppError)
}

class User {
  String name;

  User({
    required this.name,
  });
}

class AppError {
  String error;

  AppError({
    required this.error,
  });
}
2
likes
120
pub points
0%
popularity

Publisher

unverified uploader

Simple & Lightweight Error Handling Solution | Influenced by Either in Functional Programming Languages

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on dartplus