hydrated_bloc
11.0.0, released 8 months ago by bloclibrary.dev
Automatically persists and restores bloc and cubit states.
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.
- hive_ce_generator
Automatically generates TypeAdapters for Hive to store custom classes.
88Compare - stash_memory
Memory storage extension for the stash API to store vaults and caches in-memory.
30Compare
Works with
Packages commonly used together with hydrated_bloc: ones it builds on, ones built on it, and its own family.
- synchronizedhydrated_bloc depends on itLock mechanism to prevent concurrent access to asynchronous code.
- blochydrated_bloc depends on itA Dart library for implementing the BLoC pattern with Cubits and Blocs.
- hive_cehydrated_bloc depends on itA lightweight and fast NoSQL key-value database written in pure Dart.
- flutter_blocSame familyFlutter widgets to easily integrate blocs and cubits for state management.
- bloc_testSame familyA testing library that makes it easy to test blocs and cubits.
- bloc_concurrencySame familyCustom event transformers for the bloc state management package.
- replay_blocSame familyAdds automatic undo and redo support to bloc and cubit states.
- angular_blocSame familyAngularDart components that bind Bloc/Cubit state to the UI via a pipe.