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

outdated

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

example/main.dart

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

class User extends Equatable {
  final String username;

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

  @override
  List<Object> get props => [username];
}

abstract class Failure {
  String get message;

  @override
  String toString() {
    return message;
  }
}

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

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

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

  print(okResult.failure); // null
  print(okResult.success?.username); // bob
}

Future<SimpleResult<User, Failure>> fetchFromServer({bool withError}) async {
  await Future.delayed(const Duration(milliseconds: 100));
  if (withError) {
    return SimpleResult.failure(SomeFailure());
  } else {
    return SimpleResult.success(const User(username: 'bob'));
  }
}

// ignore_for_file: avoid_void_async, avoid_print
7
likes
0
pub points
81%
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