handleInitEvent method
Handles the initialization event by initializing the calculator and emitting the new state.
If the calculator can be initialized, it sets the initializing flag
to true
and attempts to initialize the calculator. Then, it emits the
new state and triggers the initialized
or initFailed
event
depending on the result.
Yields a stream of state changes.
Implementation
@protected
Stream<S> handleInitEvent() async* {
if (canInitialize) {
debugLog('Initializing calculator', debugLabel: debugLabel);
isInitializing = true;
yield currentState.copyWith(isInitializing: isInitializing) as S;
try {
await initialize();
defaultCalculatorState = await initializeDefaultCalculatorState();
final nextState = await initializeCalculatorState();
yield currentState
.merge(defaultCalculatorState)
.merge(nextState)
.copyWith(
isInitializing: isInitializing,
metadata: await loadMetadata(),
) as S;
addEvent(FastCalculatorBlocEvent.initialized<R>());
} catch (error, stacktrace) {
addEvent(FastCalculatorBlocEvent.initFailed<R>(error, stacktrace));
}
}
}