resource_result 0.0.2 copy "resource_result: ^0.0.2" to clipboard
resource_result: ^0.0.2 copied to clipboard

outdated

A package that provides a Resource class of any type. Supports the states Success, Failure and Loading.

Provides a Resource class containing data of any type and an error of any type. The result can be a Success, Loading or Failure. This allows methods to return a Resource<T> instead of T but throwing an error. Additionally, if a method returns a Future or Stream the Loading state can initially be returned.

Usage #

To return a resource from a method: #

Synchronous

Resource<int, String> parseInt(String value) {
  try {
    return Success(int.parse(value));
  } catch (e) {
    return Failure("Failed to parse value '$value'");
  }
}

Asynchronous

Stream<Resource<OtherClass, String>> convert(Stream<SomeClass> toConvert) async* {
  yield Loading(EmptyOtherClass());
  await for (final someObject in toConvert) {
    try {
      final OtherClass converted = convertToSomeObject(someObject);
      yield Success(converted);
    } catch (e) {
      yield Failure("Failed convert '$someObject'");
    }
  }
}

Use Resource result #

final Resource<int, String> result = parseInt("13");
  if (result.hasData) {
    Text("Parsed int successfully: ${result.data}");
  } else {
    Text("Failed to parse int: ${result.error}");
  }
  //or
  convert(someStream).listen((resource) {
    resource.resolve(
        onSuccess: (someClassData) => Text(
            "Converted someClass to otherClass instance successfully: $someClassData"),
        onLoading: (_) => Text("Loading..."),
        onFailure: (error) => Text("Failed to convert: $error"));
  });
1
likes
0
points
55
downloads

Publisher

unverified uploader

Weekly Downloads

A package that provides a Resource class of any type. Supports the states Success, Failure and Loading.

License

unknown (license)

More

Packages that depend on resource_result