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

outdated

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

Simple Result for Dart #

Introduction #

I needed a simple Result like Swift's Result.

I have seen people using dartz with the Either type. But for me, thats not really clear and readable.

Other solutions like result and super_enum did not fit my needs neither. So here is my solution.

Installation #

Add the following to you pubspec.yaml and replace [version] with the latest version:

dependencies:
  simple_result: ^[version]

import with

import 'package:simple_result/simple_result.dart';

Usage #

To create a Result use

Result<ValueType, FailureType>.success(value)

or

Result<ValueType, FailureType>.failure(failure)

You can then 'iterate' over success or failure with:

result.when(
	success(value) {
		//doSomeThing with success value
	},
	failure(failure) {
		//doSomeThing with failure
	}
);

You can map the Result value type with map:

Result<User, Failure>.success(user);
final stringResult = result.map((user) => user.username);  
// stringResult is of Type Result<String, Failure>

You can use convenience methods on a Result

final userResult = Result<User, Failure>.success(user);
userResult.isSuccess; // -> true
userResult.success; // -> user object.

Example #

import 'package:simple_result/simple_result.dart';
final mySuccessResult = Result<String,Failure>.success('success value');
final myErrorResult = Result<String,Failure>.failure(MyFailure());

mySuccessResult.isSuccess // -> true
mySuccessResult.success; // -> 'success value'

final myStringResult = mySuccessResult
	.when(
		success:(value) => value.toString(), 
		failure:(_) => 'ERROR');
mySuccessResult.map((value) => 'StringResult'); // maps to Result<String, Failure>()

see more in example/main.dart

7
likes
0
pub points
82%
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