Skip to content
pubscout

hydrated_bloc

11.0.0, released 8 months ago by bloclibrary.dev

Automatically persists and restores bloc and cubit states.

WASM-ready
$ flutter pub add hydrated_bloc
Source repository

What it does

Extends the bloc state management library to automatically save states to local storage and restore them upon application restart. It provides built-in persistence using Hive CE with custom serialization via toJson and fromJson methods.

AI-written summary of version 11.0.0, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You are using the bloc state management library and need to persist user sessions or application state across app restarts
  • You want automatic caching of bloc and cubit states without writing manual storage boilerplate

Skip it when

  • You are managing state using a framework other than bloc or cubit
  • You require highly complex relational database queries where a key-value store is insufficient

Strengths

  • Seamless integration with package:bloc and package:flutter_bloc
  • Platform-agnostic and performant storage layer built on Hive CE, supporting web and native platforms
  • Customizable storage directories, prefixes, and per-instance storage overrides

Weaknesses

  • Requires manual implementation of toJson and fromJson for every persisted bloc or cubit
  • Relies entirely on the bloc architecture ecosystem

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
4

Initialize HydratedBloc.storage with HydratedStorage.build during app startup before calling runApp.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  HydratedBloc.storage = await HydratedStorage.build(
    storageDirectory: kIsWeb
        ? HydratedStorageDirectory.web
        : HydratedStorageDirectory((await getTemporaryDirectory()).path),
  );
  runApp(App());
}

Alternatives

Packages that do the same job and could replace hydrated_bloc.

Works with

Packages commonly used together with hydrated_bloc: ones it builds on, ones built on it, and its own family.