hive_cubit
A lightweight Cubit that automatically
persists its state to a Hive box —
zero boilerplate, no manual box.put() calls.
Why
If you use Cubit for state management and Hive for local storage, you've
probably written the same pattern many times: read from a box on startup,
write to it on every state change. hive_cubit extracts that pattern into
a single base class.
Usage
class SettingsCubit extends HiveCubit<SettingsModel> {
SettingsCubit()
: super(
boxName: 'settings',
key: 'user_settings',
initialState: SettingsModel.initial(),
);
void updateTheme(String theme) {
updateState(state.copyWith(theme: theme));
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter(); // or Hive.init(path) on non-Flutter platforms
final settingsCubit = SettingsCubit();
await settingsCubit.ready; // waits for any persisted state to load
runApp(MyApp(settingsCubit: settingsCubit));
}
Every call to updateState(newState) does two things:
- Emits
newStateto anyBlocBuilder/BlocListenerwatching the cubit. - Writes
newStateto the Hive box underkey.
On the next app launch, the cubit reads the persisted value back before emitting its first state — no extra code required.
Notes
- Register any custom Hive
TypeAdapters forTbefore creating the cubit, the same way you would for any other Hive-stored type. Tmust be a type Hive can store directly (primitives, or a type with a registered adapter).- This package does not call
Hive.init()/Hive.initFlutter()for you — call it once during app startup, as you normally would.
License
MIT
Libraries
- hive_cubit
- A lightweight
Cubitthat auto-persists its state to a Hive box.