simple_result 0.1.1 copy "simple_result: ^0.1.1" to clipboard
simple_result: ^0.1.1 copied to clipboard

outdated

A simple posibility to return a Result with eiter success or failure.

example/main.dart

import 'package:meta/meta.dart';
import 'package:simple_result/simple_result.dart';

class User {
  final String username;

  User({@required this.username});
}

abstract class Failure {
  String get message;

  String toString() {
    return message;
  }
}

class SomeFailure extends Failure {
  final message = 'Some Failure happened';
}

void main() async {
  final okResult = await fetchFromServer(false);
  final username = okResult.when(
      success: (user) => user.username,
      failure: (failure) => "ERROR:${failure}");
  print(username); //bob

  final errorResult = await fetchFromServer(true);
  final usernameNotOk = errorResult.when(
      success: (user) => user.username,
      failure: (failure) => "ERROR:${failure}");
  print(usernameNotOk); // ERROR:Some Failure happened
}

Future<Result<User, Failure>> fetchFromServer(bool withError) async {
  await Future.delayed(Duration(milliseconds: 100));
  if (withError) {
    return Result.failure(SomeFailure());
  } else {
    return Result.success(User(username: 'bob'));
  }
}
7
likes
0
pub points
78%
popularity

Publisher

unverified uploader

A simple posibility to return a Result with eiter success or failure.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta

More

Packages that depend on simple_result