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 suggestion.

Installation

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

dependencies:
  simple_result: ^[version]

Example


abstract class Failure {}

class SomeFailure extends Failure {}

void main() async {
	final result = await fetchUser();
	final resultAsString = result.when(
		success:(user) => user.username, 
		failure:(failre) => failire.toString());
	print(resultAsString);
}
Future<Result<User,Failure>>fetchUser() async {
	if (ok){
		return Result.success(user);
	}
	return Result.failure(SomeFailure());
} 

see more in example/main.dart

Libraries

simple_result