simple_result 1.0.5 icon indicating copy to clipboard operation
simple_result: ^1.0.5 copied to clipboard

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.

This packages uses the freezed package and adds some helper methods. Thank you for this great package!

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.mapSuccesss((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

2
likes
120
pub points
73%
popularity

Publisher

unverified uploader

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

Repository (GitHub)
View/report issues

Documentation

API reference

License

Icon for licenses.MIT (LICENSE)

Dependencies

collection, equatable, freezed_annotation, meta

More

Packages that depend on simple_result