hydrated_notifier 1.0.1 copy "hydrated_notifier: ^1.0.1" to clipboard
hydrated_notifier: ^1.0.1 copied to clipboard

An extension to the state_notifier library which automatically persists and restores states.

Hydrated Notifier #

Features #

An extension to the state_notifier library which automatically persists and restores states built on top of hive.

Usage #

Setup HydratedStorage #

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  /// Initialize storage
  HydratedStateNotifier.storage = await HydratedStorage.build(
    storageDirectory: kIsWeb
        ? HydratedStorage.webStorageDirectory
        : await getTemporaryDirectory(),
  );

  runApp(App())
}

Create a HydratedStateNotifier #

class CounterNotifier extends HydratedStateNotifier<int> {
  CounterNotifier() : super(0);

  void increment() => state = state + 1;

  @override
  int fromJson(Map<String, dynamic> json) => json['value'] as int;

  @override
  Map<String, int> toJson(int state) => { 'value': state };
}

Now the CounterNotifier will automatically persist/restore their state. We can increment the counter value, hot restart, kill the app, etc... and the previous state will be retained.

Additional information #

This is a fork of hydrated_bloc.

2
likes
120
pub points
16%
popularity

Publisher

verified publisherherve-guigoz.com

An extension to the state_notifier library which automatically persists and restores states.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, hive, state_notifier, synchronized

More

Packages that depend on hydrated_notifier