Skip to content

Conversation

@meylis1998
Copy link

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 _emitted flag in BlocBase was not being set during hydration. This meant that the first emit() call after hydration would bypass the equality check (since _emitted was still false), causing duplicate state emissions.

Solution

  • Modified hydrate() to use emit() when restoring state, which properly sets the _emitted flag in BlocBase
  • Added _hydrationInProgress flag to prevent onChange from writing to storage during the hydration process (avoiding conflicts)
  • Added regression test (issue_4549_test.dart) to verify the fix

Test plan

  • All existing hydrated_bloc tests pass (73 tests)
  • New regression test validates the fix
  • Code formatted with dart format
  • Code analyzed with dart analyze --fatal-infos --fatal-warnings - no issues

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • Includes test coverage

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective
  • New and existing unit tests pass locally with my changes

…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
meylis1998 and others added 2 commits October 5, 2025 13:57
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
Copy link

codecov bot commented Oct 20, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@felangel
Copy link
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 BlocListeners being triggered by state hydration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: HydratedMixin causes BlocBase to emit the same state

2 participants