Lets say there is a Stream<Foo>
that can throw exceptions but we need to convert it to Stream<Result<Foo>>
and instead of throwing, returning a Result<Foo>
with the exception.
How can it be done?
For clarity that’s what the Result
class looks like:
sealed class Result<T> {
Result();
factory Result.success(T data) = ResultSuccess<T>;
factory Result.error(String message) = ResultError<T>;
}
final class ResultSuccess<T> extends Result<T> {
final T data;
ResultSuccess(this.data);
}
final class ResultError<T> extends Result<T> {
final String message;
ResultError(this.message);
}
2
Answers
We can use
StreamTransformer.fromHandlers
.and then use it with our initial stream:
You might also consider writing an "asResult" operator as an extension method which would enable you to then write something like…
Pseudocodish implementation follows …