What is Hydrated Bloc?
Hydrated Bloc is an extension of flutter_bloc that automatically persists your BLoC and Cubit states to disk. When the app is closed and reopened, the state is restored exactly as it was, providing a seamless offline‑first experience. It uses Hive internally to store state efficiently and supports complex state serialization.
Why Use Hydrated Bloc?
- Automatic Persistence – No manual saving/loading code in your BLoCs.
- Offline Support – State survives app restarts and is available immediately.
- Simple API – Just extend
HydratedBlocorHydratedCubitand overridefromJson/toJson. - Performance – Uses Hive, a lightweight, fast key‑value store.
- Flexible Storage – Configurable storage directory, supports custom Hive adapters.
Setting Up Hydrated Bloc
Add the required dependencies to your pubspec.yaml:
Initialize HydratedBloc before running your app. You need to provide a storage directory, typically obtained from path_provider.
Creating a Hydrated Cubit
Extend HydratedCubit instead of Cubit. Override fromJson and toJson to serialize/deserialize your state.
Creating a Hydrated Bloc
Similarly, extend HydratedBloc<Event, State> and implement the same serialization methods.
Serializing Complex State
For states with custom objects, you need to convert them to JSON‑compatible maps. Use Equatable to simplify equality checks.
Storage Configuration
You can customize storage behavior by providing your own HydratedStorage instance. For example, to use a different Hive box name or encryption.
Clearing Persisted State
To reset the state, you can clear the storage or delete the specific box.
Testing Hydrated Blocs
When testing, you can use a temporary storage directory to avoid polluting real storage.
Best Practices
- Use
Equatablefor state classes – Simplifies serialization and prevents unnecessary rebuilds. - Keep states small – Store only UI state; use a database for large datasets.
- Initialize storage early – Always call
HydratedStorage.buildbeforerunApp. - Avoid storing sensitive data – Hydrated Bloc stores data in plain text unless encrypted.
- Test serialization – Write tests that verify
toJsonandfromJsonwork correctly. - Use
copyWithmethods – Makes state updates clean and immutable. - Provide fallbacks – In
fromJson, handle missing keys gracefully.
Common Mistakes
- ❌ Forgetting to initialize storage – Causes
StateErrorwhen trying to save. ✅ Initialize beforerunApp. - ❌ Storing large objects – Slows down app startup and uses disk space. ✅ Use a database for large data; keep hydrated state minimal.
- ❌ Not handling null in
fromJson– Can throw exceptions when storage is empty. ✅ Provide default values. - ❌ Mutating state objects – Modifying state after emission can lead to inconsistencies. ✅ Always create new immutable state objects.
- ❌ Using same storage for multiple apps – Conflicts can occur. ✅ Use different storage directories per app.
Conclusion
Hydrated Bloc is an essential tool for building offline‑ready Flutter apps. With minimal code, you can persist any BLoC or Cubit state, ensuring a consistent user experience across sessions. Combine it with proper state design and testing to create robust, performant applications.