'Why separating emit() to void function made Bloc working?
I had weird problem on which I spend few hours. I have a bloc file in which I'm using dartZ to easier maintain errors. When I was using bloc like this:
NumberTriviaBloc({required this.getConcreteNumberTrivia}) : super(Empty()) {
on<GetTriviaForConcreteNumber>((event, emit) async {
final inputEither =
inputConverter.stringToUnsignedInteger(event.numberString);
inputEither.fold((failure) => emit(Error(message: invalidInputMessage)),
(integer) async {
emit(Loading());
final failureOrTrivia =
await getConcreteNumberTrivia(Params(number: integer));
emit(
failureOrTrivia.fold(
(failure) => Error(message: _mapFailureToMessage(failure)),
(trivia) => Loaded(trivia: trivia),
),
);
});
});
}
I was getting error which says I had not awaited some Future - https://pastebin.com/FWTFKYVH but it didn't seems to be true, cause I have only one function getConcreteNumberTrivia which returns Future and it is awaited.
But when I'm cut off last emit to separate function like this:
void _eitherLoadedOrErrorState(
Either<Failure, NumberTrivia> failureOrTrivia,
) {
emit(
failureOrTrivia.fold(
(failure) => Error(message: _mapFailureToMessage(failure)),
(trivia) => Loaded(trivia: trivia),
),
);
}
Code starts working properly. I don't get it, I'd just moved code to void function, which do not doing anything additional, can somebody explain me why?
Solution 1:[1]
nvoigt explained why you got an error. The reason you didn't get an error after extracting the method is that you didn't extract the emit parameter. The emit in your extracted method refers to the Bloc class's emit method, which "is only for internal use and should never be called directly outside of tests" according to the documentation.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nitrodon |
