Skip to content

feat: add cache expiration support to HydratedBloc#4631

Open
realmeylisdev wants to merge 5 commits intofelangel:masterfrom
realmeylisdev:feat/issue-4381-cache-expiration
Open

feat: add cache expiration support to HydratedBloc#4631
realmeylisdev wants to merge 5 commits intofelangel:masterfrom
realmeylisdev:feat/issue-4381-cache-expiration

Conversation

@realmeylisdev
Copy link
Copy Markdown
Contributor

Summary

Adds optional cache expiration support to HydratedBloc and HydratedCubit to automatically clear cached state after a specified duration.

Changes

  • Added optional expirationDuration parameter to HydratedBloc and HydratedCubit constructors
  • Cached state with expiration wraps data with timestamp metadata
  • Expired state returns default state and is automatically cleared from storage
  • Fully backward compatible - existing blocs work without changes
  • Handles legacy data gracefully (data without timestamp wrapper)

Storage Format

When expiration is enabled:

{
  "__hydrated_state__": { "value": 42 },
  "__hydrated_timestamp__": 1234567890
}

When expiration is disabled (default/legacy):

{ "value": 42 }

Example Usage

class CounterCubit extends HydratedCubit<int> {
  CounterCubit()
      : super(
          0,
          expirationDuration: const Duration(days: 7),
        );

  void increment() => emit(state + 1);

  @override
  int fromJson(Map<String, dynamic> json) => json['value'] as int;

  @override
  Map<String, int> toJson(int state) => {'value': state};
}

Test Coverage

  • 12 new tests covering expiration logic, edge cases, and backward compatibility
  • All 132 tests passing
  • 100% test coverage for new code

Checklist

  • Code formatted with dart format .
  • Code analyzed with dart analyze --fatal-infos --fatal-warnings .
  • All tests passing
  • Documentation updated (README.md)
  • Backward compatible

Fixes #4381

Adds optional expirationDuration parameter to HydratedBloc and HydratedCubit that allows cached state to expire after a specified duration.

Features:
- Optional expirationDuration parameter in constructors
- Expired state automatically cleared from storage
- Fully backward compatible (defaults to no expiration)
- Handles legacy data without timestamp wrapper

Fixes felangel#4381
@felangel felangel added enhancement New feature or request pkg:hydrated_bloc This issue is related to the hydrated_bloc package labels Nov 20, 2025
@felangel
Copy link
Copy Markdown
Owner

Haven't forgotten about this btw! Will take a look asap, thanks!

@felangel felangel mentioned this pull request Mar 14, 2026
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pkg:hydrated_bloc This issue is related to the hydrated_bloc package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(hydrated_bloc): add support for an expiration time on cached values

2 participants