Coder Social home page Coder Social logo

hydrz / hydrated_riverpod Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 15.0 130 KB

An extension to the riverpod state management library which automatically persists and restores riverpod states.

License: MIT License

Kotlin 0.14% Swift 0.46% Objective-C 0.04% Dart 94.87% HTML 4.49%

hydrated_riverpod's Introduction

hydrated_riverpod


Overview

hydrated_riverpod extension to the riverpod state management library which automatically persists and restores riverpod states.

Like hydrated_bloc

hydrated_riverpod exports a Storage interface which means it can work with any storage provider. Out of the box, it comes with its own implementation: HydratedStorage.

HydratedStorage is built on top of hive for a platform-agnostic, performant storage layer.

Usage

Setup HydratedRiverpod

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final storage = HydratedStorage.build(storageDirectory: ...)
  HydratedRiverpod.initialize(storage: storage);
  runApp(ProviderScope(child: MyApp()));
}

Create a HydratedStateNotifier

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

  void increment() => state++;

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

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

Now the Counter 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.

HydratedMixin

class Counter extends StateNotifier<int> with HydratedMixin {
  CounterCubit() : super(0) {
    hydrate();
  }

  void increment() => state++;

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

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

Custom Storage Directory

Any storageDirectory can be used when creating an instance of HydratedStorage:

final storage = await HydratedStorage.build(
  storageDirectory: await getApplicationDocumentsDirectory(),
);

Custom Hydrated Storage

If the default HydratedStorage doesn't meet your needs, you can always implement a custom Storage by simply implementing the Storage interface and initializing HydratedRiverpod with the custom Storage.

// my_hydrated_storage.dart

class MyHydratedStorage implements Storage {
  @override
  dynamic read(String key) {
    // TODO: implement read
  }

  @override
  Future<void> write(String key, dynamic value) async {
    // TODO: implement write
  }

  @override
  Future<void> delete(String key) async {
    // TODO: implement delete
  }

  @override
  Future<void> clear() async {
    // TODO: implement clear
  }
}
// main.dart

  HydratedRiverpod.initialize(storage: MyHydratedStorage());
  runApp(ProviderScope(child: MyApp()));

Dart Versions

  • Dart 2: >= 2.12

hydrated_riverpod's People

Contributors

hydrz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

hydrated_riverpod's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.