simple_result 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 74

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:

  simple_result: ^[version]

import with

import 'package:simple_result/simple_result.dart';

Usage #

To create a Result use

Result<ValueType, FailureType>.success(value)


Result<ValueType, FailureType>.failure(failure)

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

	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 = => 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
		success:(value) => value.toString(), 
		failure:(_) => 'ERROR'); => 'StringResult'); // maps to Result<String, Failure>()

see more in example/main.dart

0.1.2 #

  • adds support to map the result to a different value type.
  • adds isSuccess and isFailure
  • adds success getter for the value
  • adds failure getter for the failure

0.1.1 #

  • formatting again.

0.1.0 #

  • adds documentation and formats code with dartfmt.

0.0.2 #

  • changes Result<Failure,Success> to Result<Success,Failure>

0.0.1 #

  • initial release


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});

  List<Object> get props => [username];

abstract class Failure {
  String get message;

  String toString() {
    return message;

class SomeFailure extends Failure {
  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<Result<User, Failure>> fetchFromServer({bool withError}) async {
  await Future.delayed(const Duration(milliseconds: 100));
  if (withError) {
    return Result.failure(SomeFailure());
  } else {
    return Result.success(const User(username: 'bob'));

// ignore_for_file: avoid_void_async, avoid_print

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  simple_result: ^0.1.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:simple_result/simple_result.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 29, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
meta ^1.1.8 1.1.8
Dev dependencies
equatable ^1.1.0
lint ^1.1.1
test ^1.12.0