Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/felangel/hydrated_bloc
An extension to the bloc state management library which automatically persists and restores bloc states.
https://github.com/felangel/hydrated_bloc
cache dart dart-library dart-package dartlang flutter flutter-package persistence state state-management
Last synced: about 1 month ago
JSON representation
An extension to the bloc state management library which automatically persists and restores bloc states.
- Host: GitHub
- URL: https://github.com/felangel/hydrated_bloc
- Owner: felangel
- License: mit
- Archived: true
- Created: 2019-05-26T21:58:44.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-09T03:23:09.000Z (over 4 years ago)
- Last Synced: 2024-09-25T19:19:11.656Z (about 1 month ago)
- Topics: cache, dart, dart-library, dart-package, dartlang, flutter, flutter-package, persistence, state, state-management
- Language: Dart
- Homepage: https://pub.dev/packages/hydrated_bloc
- Size: 1.73 MB
- Stars: 191
- Watchers: 13
- Forks: 23
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
⚠️ Attention: This repository has been moved to https://github.com/felangel/bloc and is now read-only!
An extension to the [bloc state management library](https://github.com/felangel/bloc) which automatically persists and restores bloc states and is built on top of [hydrated_cubit](https://pub.dev/packages/hydrated_cubit).
## Overview
`hydrated_bloc` 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 [path_provider](https://pub.dev/packages/path_provider) for a platform-agnostic storage layer. The out-of-the-box storage implementation reads/writes to file using the `toJson`/`fromJson` methods on `HydratedBloc` and should perform very well for most use-cases (performance reports coming soon). `HydratedStorage` is supported for desktop ([example](https://github.com/felangel/hydrated_bloc/tree/master/example)).
## Usage
### 1. Use `HydratedStorage`
```dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
HydratedBloc.storage = await HydratedStorage.build();
runApp(App());
}
```### 2. Extend `HydratedBloc` and override `fromJson`/`toJson`
```dart
enum CounterEvent { increment, decrement }class CounterBloc extends HydratedBloc {
CounterBloc() : super(0);@override
Stream mapEventToState(CounterEvent event) async* {
switch (event) {
case CounterEvent.decrement:
yield state - 1;
break;
case CounterEvent.increment:
yield state + 1;
break;
}
}@override
int fromJson(Map json) => json['value'] as int;@override
Map toJson(int state) => { 'value': state };
}
```Now our `CounterBloc` is a `HydratedBloc` and will automatically persist its state. We can increment the counter value, hot restart, kill the app, etc... and our `CounterBloc` will always retain its state.
## Custom Storage Directory
By default, all data is written to [temporary storage](https://github.com/flutter/plugins/blob/61c39d1e79e8f36030162a5f85fb491c65f4e51c/packages/path_provider/lib/path_provider.dart#L24) which means it can be wiped by the operating system at any point in time.
An optional `storageDirectory` can be provided to override the default temporary storage directory:
```dart
HydratedBloc.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 `HydratedBloc` with the custom `Storage`.
```dart
// my_hydrated_storage.dartclass MyHydratedStorage implements Storage {
@override
dynamic read(String key) {
// TODO: implement read
}@override
Future write(String key, dynamic value) async {
// TODO: implement write
}@override
Future delete(String key) async {
// TODO: implement delete
}@override
Future clear() async {
// TODO: implement clear
}
}
``````dart
// main.dartHydratedBloc.storage = MyHydratedStorage();
```## Maintainers
- [Felix Angelov](https://github.com/felangel)
## Supporters
[](https://verygood.ventures)
## Starware
Hydrated Bloc is Starware.
This means you're free to use the project, as long as you star its GitHub repository.
Your appreciation makes us grow and glow up. ⭐