hydrated_bloc 0.4.1

Build Status Code Coverage Pub Package MIT License

An extension to the bloc state management library which automatically persists and restores bloc states.

Usage #

1. Use HydratedBlocDelegate

void main() async {
  BlocSupervisor.delegate = await HydratedBlocDelegate.build();

2. Extend HydratedBloc and override initialState, fromJson and toJson methods

class CounterBloc extends HydratedBloc<CounterEvent, CounterState> {
  // Use previously cached initialState if it's available
  CounterState get initialState {
    return super.initialState ?? CounterState(0);

  // Called when trying to read cached state from storage.
  // Be sure to handle any exceptions that can occur and return null
  // to indicate that there is no cached data.
  CounterState fromJson(Map<String, dynamic> source) {
    try {
      return CounterState(source['value'] as int);
    } catch (_) {
      return null;

  // Called on each state change (transition)
  // If it returns null, then no cache updates will occur.
  // Otherwise, the returned value will be cached.
  Map<String, int> toJson(CounterState state) {
    try {
      return { 'value': state.value };
    } catch (_) {
      return null;

  Stream<CounterState> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.decrement:
        yield CounterState(currentState.value - 1);
      case CounterEvent.increment:
        yield CounterState(currentState.value + 1);

enum CounterEvent { increment, decrement }

class CounterState {
  int value;


Now our CounterBloc is a HydratedBloc and will automatically persist its state. We can increment the counter value, hot restart, kill the app, etc... and our CounterBloc will always retain its state.

How it works #

Overview #

hydrated_bloc exports a HydratedStorage interface which means it can work with any storage provider. Out of the box, it comes with its own implementation: HydratedBlocStorage.

HydratedBlocStorage is built on top of path_provider for a platform-agnostic storage layer. The out-of-the-box storage implementation reads/writes to file using the toJson/fromJson methods on HydratedBloc and should perform very well for most use-cases (performance reports coming soon).

Considerations #

All data is written to temporary storage which means it can be wiped by the operating system at any point in time. As a result, hydrated_bloc is not intended to be used as a persistent database and should be viewed as a cache instead.

In addition, while the HydratedBlocStorage client doesn't automatically encrypt/decrypt the data, it is fairly straightforward to implement a custom HydratedStorage client which does support encryption.

0.4.1 #

  • Update to support optional id in cases where there are multiple instances of the same HydratedBloc
  • Documentation Updates

0.4.0 #

  • Update to bloc v0.15.0
  • Documentation Updates

0.3.2 #

  • Minor Updates to Package Dependencies
  • Documentation Updates

0.3.1 #

  • Add guards to HydratedBlocStorage to prevent exception if cache is corrupt.

0.3.0 #

  • Update HydratedBlocStorage to use getTemporaryDirectory instead of getApplicationDocumentsDirectory
  • Documentation Updates

0.2.1 #

  • Bugfix to handle Blocs alongside HydrateBlocs within the same application.
  • toJson can return null to avoid persisting the state change

0.2.0 #

  • Upated HydrateBlocDelegate to have a static build
  • Updated toJson and fromJson to eliminate the need to call json.encode and json.decode explicitly.
  • HydratedBlocSharedPreferences replaced with HydratedBlocStorage
  • Removed dependency on SharedPreferences
  • Documentation Updates

0.1.0 #

  • Renamed HydratedBlocSharedPreferences to HydratedSharedPreferences
  • Documentation Updates

0.0.3 #

Added clear to HydratedBlocStorage API and Documentation Updates

0.0.2 #

Documentation Updates

0.0.1 #

Initial Version of the library.


  • HydratedBloc
  • HydratedBlocDelegate
  • HydratedBlocSharedPreferences


example #

A new Flutter project.

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  hydrated_bloc: ^0.4.1

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:hydrated_bloc/hydrated_bloc.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Sep 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
bloc ^0.15.0 0.15.0
flutter 0.0.0
path_provider ^1.1.0 1.3.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
platform 2.2.1
rxdart 0.22.2
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_bloc ^0.21.0
mockito ^4.0.0