{"id":15008491,"url":"https://github.com/felangel/hydrated_bloc","last_synced_at":"2025-10-03T16:31:38.141Z","repository":{"id":66107948,"uuid":"188735618","full_name":"felangel/hydrated_bloc","owner":"felangel","description":"An extension to the bloc state management library which automatically persists and restores bloc states.","archived":true,"fork":false,"pushed_at":"2020-07-09T03:23:09.000Z","size":1810,"stargazers_count":191,"open_issues_count":0,"forks_count":23,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-09-29T16:03:55.413Z","etag":null,"topics":["cache","dart","dart-library","dart-package","dartlang","flutter","flutter-package","persistence","state","state-management"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/hydrated_bloc","language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/felangel.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["felangel"]}},"created_at":"2019-05-26T21:58:44.000Z","updated_at":"2024-09-10T13:22:37.000Z","dependencies_parsed_at":null,"dependency_job_id":"bd1b3597-6b7a-4db6-ba8c-74c42e92eb00","html_url":"https://github.com/felangel/hydrated_bloc","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felangel%2Fhydrated_bloc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felangel%2Fhydrated_bloc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felangel%2Fhydrated_bloc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/felangel%2Fhydrated_bloc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/felangel","download_url":"https://codeload.github.com/felangel/hydrated_bloc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":235156000,"owners_count":18944824,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","dart","dart-library","dart-package","dartlang","flutter","flutter-package","persistence","state","state-management"],"created_at":"2024-09-24T19:19:04.460Z","updated_at":"2025-10-03T16:31:37.520Z","avatar_url":"https://github.com/felangel.png","language":"Dart","funding_links":["https://github.com/sponsors/felangel"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003cb\u003e⚠️ Attention: This repository has been moved to https://github.com/felangel/bloc and is now read-only!\u003c/b\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://github.com/felangel/hydrated_bloc/raw/master/doc/assets/hydrated_bloc_logo.png\" height=\"100\" alt=\"Hydrated Bloc\"\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://pub.dev/packages/hydrated_bloc\"\u003e\n    \u003cimg src=\"https://img.shields.io/pub/v/hydrated_bloc.svg\" alt=\"Pub Version\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/felangel/hydrated_bloc/actions\"\u003e\n    \u003cimg src=\"https://github.com/felangel/hydrated_bloc/workflows/build/badge.svg\" alt=\"Build Status\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://codecov.io/gh/felangel/hydrated_bloc\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/felangel/hydrated_bloc/branch/master/graph/badge.svg\" alt=\"Code Coverage\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://pub.dev/packages/effective_dart\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/style-effective_dart-40c4ff.svg\" alt=\"style: effective dart\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/license-MIT-blue.svg\" alt=\"MIT License\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/zepfietje/starware\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Starware-⭐-black?labelColor=f9b00d\" alt=\"Starware\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/felangel/bloc\"\u003e\n    \u003cimg src=\"https://tinyurl.com/bloc-library\" alt=\"Bloc Library\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nAn 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).\n\n## Overview\n\n`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`.\n\n`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)).\n\n## Usage\n\n### 1. Use `HydratedStorage`\n\n```dart\nvoid main() async {\n  WidgetsFlutterBinding.ensureInitialized();\n  HydratedBloc.storage = await HydratedStorage.build();\n  runApp(App());\n}\n```\n\n### 2. Extend `HydratedBloc` and override `fromJson`/`toJson`\n\n```dart\nenum CounterEvent { increment, decrement }\n\nclass CounterBloc extends HydratedBloc\u003cCounterEvent, int\u003e {\n  CounterBloc() : super(0);\n\n  @override\n  Stream\u003cint\u003e mapEventToState(CounterEvent event) async* {\n    switch (event) {\n      case CounterEvent.decrement:\n        yield state - 1;\n        break;\n      case CounterEvent.increment:\n        yield state + 1;\n        break;\n    }\n  }\n\n  @override\n  int fromJson(Map\u003cString, dynamic\u003e json) =\u003e json['value'] as int;\n\n  @override\n  Map\u003cString, int\u003e toJson(int state) =\u003e { 'value': state };\n}\n```\n\nNow 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.\n\n## Custom Storage Directory\n\nBy 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.\n\nAn optional `storageDirectory` can be provided to override the default temporary storage directory:\n\n```dart\nHydratedBloc.storage = await HydratedStorage.build(\n  storageDirectory: await getApplicationDocumentsDirectory(),\n);\n```\n\n## Custom Hydrated Storage\n\nIf 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`.\n\n```dart\n// my_hydrated_storage.dart\n\nclass MyHydratedStorage implements Storage {\n  @override\n  dynamic read(String key) {\n    // TODO: implement read\n  }\n\n  @override\n  Future\u003cvoid\u003e write(String key, dynamic value) async {\n    // TODO: implement write\n  }\n\n  @override\n  Future\u003cvoid\u003e delete(String key) async {\n    // TODO: implement delete\n  }\n\n  @override\n  Future\u003cvoid\u003e clear() async {\n    // TODO: implement clear\n  }\n}\n```\n\n```dart\n// main.dart\n\nHydratedBloc.storage = MyHydratedStorage();\n```\n\n## Maintainers\n\n- [Felix Angelov](https://github.com/felangel)\n\n## Supporters\n\n[\u003cimg src=\"https://raw.githubusercontent.com/felangel/bloc/master/docs/assets/vgv_logo.png\" width=\"120\" /\u003e](https://verygood.ventures)\n\n## Starware\n\nHydrated Bloc is Starware.  \nThis means you're free to use the project, as long as you star its GitHub repository.  \nYour appreciation makes us grow and glow up. ⭐\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelangel%2Fhydrated_bloc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffelangel%2Fhydrated_bloc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffelangel%2Fhydrated_bloc/lists"}