fix(hydrated_bloc): prevent duplicate emissions after hydration#4630
Open
meylis1998 wants to merge 6 commits intofelangel:masterfrom
Open
fix(hydrated_bloc): prevent duplicate emissions after hydration#4630meylis1998 wants to merge 6 commits intofelangel:masterfrom
meylis1998 wants to merge 6 commits intofelangel:masterfrom
Conversation
…ngel#4549) When a HydratedBloc/HydratedCubit was restored from storage, emitting the same state would incorrectly emit it to the stream, violating the documented behavior that emit() does nothing if the state equals the current state. This occurred because the _emitted flag in BlocBase was not set during hydration. The fix: - Modified hydrate() to use emit() when restoring state, properly setting the _emitted flag in BlocBase - Added _hydrationInProgress flag to prevent onChange from writing to storage during the hydration process - Added regression test to verify the fix Fixes felangel#4549
Move test CounterCubit class into its own file following the prefer_file_naming_conventions lint rule. This matches the existing pattern used in other test files (e2e_test.dart).
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Owner
|
Thanks for the PR! I need to think about this a bit more carefully because this will potentially break any apps which depend on |
Contributor
Author
|
Hey @felangel, would you mind reviewing this? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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