simple_result 0.1.9

Dart native js
Flutter Android iOS web

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

SimpleResult<ValueType, FailureType>.success(value)

or

SimpleResult<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:

SimpleResult<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 = SimpleResult<User, Failure>.success(user);
userResult.isSuccess; // -> true
userResult.success; // -> user object.

Example #

import 'package:simple_result/simple_result.dart';
final mySuccessResult = SimpleResult<String,Failure>.success('success value');
final myErrorResult = SimpleResult<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

0
likes
100
pub points
52%
popularity

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

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

cfregin@spielgestalt.de

License

MIT (LICENSE)

Dependencies

equatable, meta

More

Packages that depend on simple_result