bloc_signals_hydrate 0.1.6 copy "bloc_signals_hydrate: ^0.1.6" to clipboard
bloc_signals_hydrate: ^0.1.6 copied to clipboard

State persistence and hydration adapters for BlocSignal state containers.

example/example.dart

import 'package:bloc_signals_hydrate/bloc_signals_hydrate.dart';

/// A sample hydrated cubit storing an integer counter.
class CounterCubit extends HydratedCubitSignal<int> {
  CounterCubit() : super(initialState: 0);

  void increment() => emit(stateValue + 1);

  @override
  int? fromJson(dynamic json) => json as int?;

  @override
  dynamic toJson(int state) => state;
}

void main() async {
  // Set up in-memory hydrated storage
  final storage = MemoryHydratedStorage();
  HydratedStorage.storage = storage;

  // Pre-seed storage key
  storage.write('CounterCubit', 42);

  // Instantiating cubit synchronously restores state = 42
  final cubit = CounterCubit();
  print('Restored counter state: ${cubit.stateValue}'); // 42

  // Emitting increments state and updates storage
  cubit.increment();
  print('New counter state: ${cubit.stateValue}'); // 43
  print('Stored value: ${storage.read("CounterCubit")}'); // 43

  // Clear storage and reset state
  await cubit.clear();
  print('Cleared counter state: ${cubit.stateValue}'); // 0

  await cubit.close();
}
0
likes
0
points
543
downloads

Publisher

verified publisherstonehenge.com

Weekly Downloads

State persistence and hydration adapters for BlocSignal state containers.

Repository (GitHub)
View/report issues

Topics

#bloc #signals #hydrated #persistence

License

unknown (license)

Dependencies

bloc_signals, meta, signals_core

More

Packages that depend on bloc_signals_hydrate