fix(hydrated_bloc): prevent duplicate emissions after hydration #4630
+92
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #4549
When a HydratedBloc/HydratedCubit is restored from storage, emitting the same state now correctly does nothing instead of emitting it to the stream.
Problem
The issue occurred because the
_emittedflag inBlocBasewas not being set during hydration. This meant that the firstemit()call after hydration would bypass the equality check (since_emittedwas stillfalse), causing duplicate state emissions.Solution
hydrate()to useemit()when restoring state, which properly sets the_emittedflag inBlocBase_hydrationInProgressflag to preventonChangefrom writing to storage during the hydration process (avoiding conflicts)issue_4549_test.dart) to verify the fixTest plan
dart formatdart analyze --fatal-infos --fatal-warnings- no issuesType of Change
Checklist