In my bloc state I have this:
Option<Either<T, Stream<T>>> optionFailureOrStream,
It' can be empty ( Option
) or have an error
or a stream
.
In my bloc
I yielding this with optionOf
like this:
yield state.copyWith(optionFailureOrStream: optionOf(failureOrStream));
Now in my Widget I want to know if it has a value and if it has a value, I want to fold them to return two different screens:
if (state.optionFailureOrStream.isSome()) {
final optionFailureOrStream = state.optionFailureOrStream;
optionFailureOrStream.map((a) => a.fold(
(failure) => FailureWidget(failure: failure),
(stream) => buildSaved(stream, context)));
}
But somehow I can't get rid of the Option<T>
type. It complains about the wrong type.
The return type 'Option<Widget>' isn't a 'Widget', as required by the closure's context.dart(return_of_invalid_type_from_closure)
I assume there is a really easy way to do this. I am having a real hard time finding any dart/flutter related manuals and the source code of dartz
isn't that self-explaining, if you are new to these concepts.
Widget func() {
Option<Either<String, Stream<String>>>//I assume your failure is a string for example
optionFailureOrStream;
return optionFailureOrStream.fold(
() => Container(),//if none()
(Either<String, Stream<String>> either)//if some(Either<String,Stream<String>>)
=> either.fold(
(String failure) => Container(),
(Stream<String> stream) => Container(),
),
);
}
note: this code is just for demonstration (if that is not clear), as you see I am not even initializing the variable but the code is just to demonstrate how you can return a value from an option without using an if statement
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.