hive_cubit 0.1.1
hive_cubit: ^0.1.1 copied to clipboard
A lightweight HiveCubit that auto-persists Cubit state to a Hive box with zero boilerplate.
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(),
);
// updateState returns a Future — await it if you want to be sure
// the write to disk has completed before moving on.
Future<void> updateTheme(String theme) {
return 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
newStateimmediately to anyBlocBuilder/BlocListenerwatching the cubit. - Waits for the underlying box to be ready, then writes
newStateto the Hive box underkey.
Because step 2 is asynchronous, updateState returns a Future<void>.
You don't have to await it — the emit already happened synchronously — but
awaiting it guarantees the write finished, which is useful right before
closing the app or navigating away.
On the next app launch, the cubit reads the persisted value back before
emitting its first state — no extra code required. See
example/hive_cubit_example.dart for a
runnable, step-by-step walkthrough of this loading behavior.
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. - Closing a
HiveCubit(viaclose()) does not close its underlying Hive box, since the box may be shared elsewhere in your app. Close boxes explicitly withHive.box(name).close()if and when appropriate. - If the box fails to open,
HiveCubitthrows aHiveCubitExceptionwith a descriptive message instead of a rawHiveError.
License #
MIT