simply_result 1.0.0 copy "simply_result: ^1.0.0" to clipboard
simply_result: ^1.0.0 copied to clipboard

A lightweight Result type for Dart with functional helpers, async support, and zero dependencies.

simply_result #

A lightweight Result type for Dart.

style: very good analysis coverage


Features #

  • Functional Result type
  • Success / Error modeling
  • Functional helpers (map, flatMap, zip)
  • Async helpers
  • Zero dependencies
  • High test coverage

API Index #

Quick dictionary of all helpers available in simply_result.

Installation #

dependencies:
  simply_result: ^1.0.0

API #

success(value) #

Creates a successful result containing a value.

Use it when an operation completes correctly.

final result = success(10);

print(result);
// → Success(10)

error(err) #

Creates a failed result containing an error.

Useful for returning failures without throwing exceptions.

final result = error("network failure");

print(result);
// → Error(network failure)

map #

Transforms the value only if the result is Success. If the result is Error, it is propagated unchanged.

final result = success(5)
    .map((v) => v * 2);

print(result);
// → Success(10)

Error propagation:

final result = error("fail")
    .map((v) => v * 2);

print(result);
// → Error(fail)

flatMap #

Chains operations that also return a Res.

Useful for composing multiple fallible operations.

final result = success(5)
    .flatMap((v) => success(v * 2));

print(result);
// → Success(10)

Error propagation:

final result = error("fail")
    .flatMap((v) => success(v * 2));

print(result);
// → Error(fail)

mapError #

Transforms the error value without touching the success value.

final result = error("network")
    .mapError((e) => "Error: $e");

print(result);
// → Error(Error: network)

fold #

Handles both success and error cases, returning a single value.

final result = success(10).fold(
  (err) => "Error: $err",
  (value) => "Value: $value",
);

print(result);
// → Value: 10

With error:

final result = error("fail").fold(
  (err) => "Error: $err",
  (value) => "Value: $value",
);

print(result);
// → Error: fail

getOrElse #

Returns the value if success, otherwise returns a fallback.

final value = success(10)
    .getOrElse(() => 0);

print(value);
// → 10

With error:

final value = error("fail")
    .getOrElse(() => 0);

print(value);
// → 0

zip #

Combines two results into one.

If any result is Error, the first error is returned.

final result = Res.zip(
  success(2),
  success(3),
  (a, b) => a + b,
);

print(result);
// → Success(5)

With error:

final result = Res.zip(
  success(2),
  error("fail"),
  (a, b) => a + b,
);

print(result);
// → Error(fail)

zip3 #

Combines three results.

final result = Res.zip3(
  success(2),
  success(3),
  success(5),
  (a, b, c) => a + b + c,
);

print(result);
// → Success(10)

mapAsync #

Asynchronous version of map.

final result = await success(5)
    .mapAsync((v) async => v * 2);

print(result);
// → Success(10)

flatMapAsync #

Asynchronous version of flatMap.

final result = await success(5)
    .flatMapAsync((v) async => success(v * 2));

print(result);
// → Success(10)

pipe #

Allows chaining transformations in a functional pipeline style.

final result = success(5)
    .pipe((r) => r.map((v) => v * 2));

print(result);
// → Success(10)

Res.unit() #

Creates a success result containing null. Useful when the operation has no meaningful return value.

final result = Res.unit();

print(result);
// → Success(null)
2
likes
0
points
265
downloads

Publisher

unverified uploader

Weekly Downloads

A lightweight Result type for Dart with functional helpers, async support, and zero dependencies.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, res

More

Packages that depend on simply_result