{"id":17238358,"url":"https://github.com/hoc081098/rx_shared_preferences","last_synced_at":"2025-12-12T05:59:46.739Z","repository":{"id":35056300,"uuid":"181147911","full_name":"hoc081098/rx_shared_preferences","owner":"hoc081098","description":"🌀 Shared preferences with RxDart Stream observation ⚡️ Reactive shared preferences for Flutter 🌸Reactive stream wrapper around SharedPreferences 🍄 Lightweight and easy-to-use 🌱 A reactive key-value store for Flutter projects. Like shared_preferences, but with Streams 📕 Rx Shared Preferences for Flutter 🌿 rx_shared_preferences 🌰 rx_shared_preference 🍷 Reactive SharedPreferences for Flutter  🌰 A stream based wrapper over shared_preferences, allowing reactive key-value storage.","archived":false,"fork":false,"pushed_at":"2025-03-25T01:28:09.000Z","size":54114,"stargazers_count":44,"open_issues_count":9,"forks_count":10,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T11:04:28.608Z","etag":null,"topics":["flutter-bloc","flutter-bloc-pattern","flutter-bloc-pattern-rxdart","flutter-bloc-rxdart","flutter-package","flutter-reactive","flutter-rx","flutter-rx-preferences","flutter-rxdart","flutter-stream","flutter-stream-preferences","rx-preferences","rx-shared-preferences","rxdart","rxdart-helper","stream-observation","stream-preferences","stream-shared-preferences"],"latest_commit_sha":null,"homepage":"https://pub.dev/packages/rx_shared_preferences","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/hoc081098.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["hoc081098"],"custom":["https://www.buymeacoffee.com/hoc081098"]}},"created_at":"2019-04-13T09:19:24.000Z","updated_at":"2025-03-23T04:59:39.000Z","dependencies_parsed_at":"2023-10-16T03:15:38.153Z","dependency_job_id":"44245ae3-3969-4683-965b-56ae4fc2a5ca","html_url":"https://github.com/hoc081098/rx_shared_preferences","commit_stats":{"total_commits":239,"total_committers":6,"mean_commits":"39.833333333333336","dds":0.09205020920502094,"last_synced_commit":"1f649d25536ab4f7a912fc2106d2eb614b72b2c3"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2Frx_shared_preferences","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2Frx_shared_preferences/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2Frx_shared_preferences/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hoc081098%2Frx_shared_preferences/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hoc081098","download_url":"https://codeload.github.com/hoc081098/rx_shared_preferences/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036067,"owners_count":21037092,"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":["flutter-bloc","flutter-bloc-pattern","flutter-bloc-pattern-rxdart","flutter-bloc-rxdart","flutter-package","flutter-reactive","flutter-rx","flutter-rx-preferences","flutter-rxdart","flutter-stream","flutter-stream-preferences","rx-preferences","rx-shared-preferences","rxdart","rxdart-helper","stream-observation","stream-preferences","stream-shared-preferences"],"created_at":"2024-10-15T05:45:23.824Z","updated_at":"2025-12-12T05:59:46.678Z","avatar_url":"https://github.com/hoc081098.png","language":"Dart","funding_links":["https://github.com/sponsors/hoc081098","https://www.buymeacoffee.com/hoc081098"],"categories":[],"sub_categories":[],"readme":"# rx_shared_preferences ![alt text](https://avatars3.githubusercontent.com/u/6407041?s=32\u0026v=4)\n\n## Author: [Petrus Nguyễn Thái Học](https://github.com/hoc081098)\n\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/0eada0cc95ce4adeb49ace73d5adf15d)](https://app.codacy.com/gh/hoc081098/rx_shared_preferences?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=hoc081098/rx_shared_preferences\u0026utm_campaign=Badge_Grade_Settings)\n[![Pub](https://img.shields.io/pub/v/rx_shared_preferences.svg)](https://pub.dartlang.org/packages/rx_shared_preferences)\n[![Pub](https://img.shields.io/pub/v/rx_shared_preferences.svg?include_prereleases)](https://pub.dartlang.org/packages/rx_shared_preferences)\n[![codecov](https://codecov.io/gh/hoc081098/rx_shared_preferences/branch/master/graph/badge.svg)](https://codecov.io/gh/hoc081098/rx_shared_preferences)\n[![Build example](https://github.com/hoc081098/rx_shared_preferences/actions/workflows/build-example.yml/badge.svg)](https://github.com/hoc081098/rx_shared_preferences/actions/workflows/build-example.yml)\n![Flutter CI](https://github.com/hoc081098/rx_shared_preferences/workflows/Flutter%20CI/badge.svg)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Style](https://img.shields.io/badge/style-lints-40c4ff.svg)](https://pub.dev/packages/lints)\n[![Hits](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2Fhoc081098%2Frx_shared_preferences\u0026count_bg=%2379C83D\u0026title_bg=%23555555\u0026icon=\u0026icon_color=%23E7E7E7\u0026title=hits\u0026edge_flat=false)](https://hits.seeyoufarm.com)\n\n\u003c!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --\u003e\n[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)\n\u003c!-- ALL-CONTRIBUTORS-BADGE:END --\u003e\n\n-   Shared preference with `rxdart` Stream observation.\n-   Reactive shared preferences for `Flutter`.\n-   Reactive stream wrapper around SharedPreferences.\n-   This package provides reactive shared preferences interaction with very little code. It is designed specifically to be used with Flutter and Dart.\n\n## Buy me a coffee\n\nLiked some of my work? Buy me a coffee (or more likely a beer)\n\n\u003ca href=\"https://www.buymeacoffee.com/hoc081098\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/v2/default-blue.png\" alt=\"Buy Me A Coffee\" height=64\u003e\u003c/a\u003e\n\n## Note\n\nSince version `1.3.4`, this package is an extension of [rx_storage](https://github.com/Flutter-Dart-Open-Source/rx_storage) package.\n\n## More details about the returned `Stream`\n\n-   It's a **single-subscription `Stream`** (i.e. it can only be listened once).\n\n-   `Stream` will emit the **value (nullable)** or **a `TypeError`** as its first event when it is listened to.\n\n-   It will **automatically** emit the new value when the value associated with key was changed successfully\n    (it will also **emit `null`** when value associated with key was `removed` or set to `null`).\n\n-   When value read from Storage has a type other than expected type:\n    -   If value is `null`, the `Stream` will **emit `null`** (this occurred because `null` can be cast to any nullable type).\n    -   Otherwise, the `Stream` will **emit a `TypeError`**.\n\n-   **Can emit** two consecutive data events that are equal.\n    You should use Rx operator like `distinct`\n    (more commonly known as `distinctUntilChanged` in other Rx implementations)\n    to create a `Stream` where data events are skipped if they are equal to the previous data event.\n\n```text\nKey changed   |----------K1---K2------K1----K1-----K2---------\u003e time\n              |                                                \nValue stream  |-----@----@------------@-----@-----------------\u003e time\n              |    ^                                      \n              |    |\n              |  Listen(key=K1)\n              |\n              |  @: nullable value or TypeError\n```\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://github.com/hoc081098/rx_shared_preferences/raw/master/rx_prefs.png\" width=\"700\"\u003e\n\u003c/p\u003e\n\n## Getting Started\n\nIn your flutter project, add the dependency to your `pubspec.yaml`\n\n```yaml\ndependencies:\n  [...]\n  rx_shared_preferences: \u003clatest_version\u003e\n```\n\n## Usage\n\n### 1. Import and instantiate\n\n-   Import `rx_shared_preferences`.\n\n```dart\nimport 'package:rx_shared_preferences/rx_shared_preferences.dart';\n```\n\n-   Wrap your `SharedPreferences` in a `RxSharedPreferences`.\n\n```dart\n// via constructor.\nfinal rxPrefs = RxSharedPreferences(await SharedPreferences.getInstance()); // use await\nfinal rxPrefs = RxSharedPreferences(SharedPreferences.getInstance());       // await is optional\nfinal rxPrefs = RxSharedPreferences.getInstance();                          // default singleton instance\n\n// via extension.\nfinal rxPrefs = (await SharedPreferences.getInstance()).rx;                 // await is required\n```\n\n\u003e NOTE: When using `RxSharedPreferences.getInstance()` and extension `(await SharedPreferences.getInstance()).rx`, \n\u003e to config the logger, you can use `RxSharedPreferencesConfigs.logger` setter.\n\n### 2. Add a logger (optional)\n\nYou can pass a logger to the optional parameter of `RxSharedPreferences` constructor.\nThe logger will log messages about operations (such as read, write, ...) and stream events.\nThis package provides two `RxSharedPreferencesLogger`s: \n-   `RxSharedPreferencesDefaultLogger`. \n-   `RxSharedPreferencesEmptyLogger`.\n\n```dart\nfinal rxPrefs = RxSharedPreferences(\n  SharedPreferences.getInstance(),\n  kReleaseMode ? null : RxSharedPreferencesDefaultLogger(),\n  // disable logging when running in release mode.\n);\n```\n\n\u003e NOTE: To disable logging when running in release mode, you can pass `null` or `const RxSharedPreferencesEmptyLogger()` \n\u003e to `RxSharedPreferences` constructor, or use `RxSharedPreferencesConfigs.logger` setter.\n\n\u003e NOTE: To prevent printing `↓ Disposed successfully → DisposeBag#...`.\n\u003e ```dart\n\u003e import 'package:disposebag/disposebag.dart' show DisposeBagConfigs;\n\u003e void main() {\n\u003e   DisposeBagConfigs.logger = null;\n\u003e }\n\u003e ```\n\n### 3. Select stream and observe\n\n-   Then, just listen `Stream`s, transform `Stream`s through operators such as `map`, `flatMap`, etc...\n-   If you need to listen to the `Stream` many times, you can use broadcast operators such as `share`, `shareValue`, `publish`, `publishValue`, etc...\n\n```dart\n// Listen\nrxPrefs.getStringListStream('KEY_LIST').listen(print);                  // [*]\n\n// Broadcast stream\nrxPrefs.getStringListStream('KEY_LIST').share();\nrxPrefs.getStringListStream('KEY_LIST').shareValue();\nrxPrefs.getStringListStream('KEY_LIST').asBroadcastStream();\n\n// Transform stream\nrxPrefs.getIntStream('KEY_INT')\n  .map((i) =\u003e /* Do something cool */)\n  .where((i) =\u003e /* Filtering */)\n  ...\n\n// must **use same rxPrefs** instance when set value and select stream\nawait rxPrefs.setStringList('KEY_LIST', ['Cool']);                      // [*] will print ['Cool']\n```\n\n-   In the previous example, we re-used the `RxSharedPreferences` object `rxPrefs` for all operations.\n    All operations must go through this object in order to correctly notify subscribers.\n    Basically, you must use the same `RxSharedPreferences` instance when set value and select stream.\n\n-   In a Flutter app, you can:\n    -   Create a global `RxSharedPreferences` instance.\n        \n    -   Use the default singleton instance via `RxSharedPreferences.getInstance()`.\n        \n    -   Use `InheritedWidget`/`Provider` to provide a `RxSharedPreferences` instance (create it in `main` function) for all widgets (recommended). \n        See [example/main](https://github.com/hoc081098/rx_shared_preferences/blob/95642a7fe8e8e0ad4579d7ae084aec9a10fe6dff/example/lib/main.dart#L17).\n\n```dart\n// An example for wrong usage.\n\nrxPrefs1.getStringListStream('KEY_LIST').listen(print); // [*]\n\nrxPrefs2.setStringList('KEY_LIST', ['Cool']);           // [*] will not print anything,\n                                                        // because rxPrefs1 and rxPrefs2 are different instances.\n```\n\n### 4. Stream APIs and RxStorage APIs\n\n-   All `Stream`s APIs (via extension methods).\n\n```dart\n  Stream\u003cObject?\u003e              getObjectStream(String key, [Decoder\u003cObject?\u003e? decoder]);\n  Stream\u003cbool?\u003e                getBoolStream(String key);\n  Stream\u003cdouble?\u003e              getDoubleStream(String key);\n  Stream\u003cint?\u003e                 getIntStream(String key);\n  Stream\u003cString?\u003e              getStringStream(String key);\n  Stream\u003cList\u003cString\u003e?\u003e        getStringListStream(String key);\n  Stream\u003cSet\u003cString\u003e\u003e          getKeysStream();\n\n  Future\u003cvoid\u003e                 updateBool(String key, Transformer\u003cbool?\u003e transformer);\n  Future\u003cvoid\u003e                 updateDouble(String key, Transformer\u003cdouble?\u003e transformer);\n  Future\u003cvoid\u003e                 updateInt(String key, Transformer\u003cint?\u003e transformer);\n  Future\u003cvoid\u003e                 updateString(String key, Transformer\u003cString?\u003e transformer);\n  Future\u003cvoid\u003e                 updateStringList(String key, Transformer\u003cList\u003cString\u003e?\u003e transformer);\n```\n\n-   All methods from [RxStorage](https://pub.dev/documentation/rx_storage/latest/rx_storage/RxStorage-class.html) \n    (because `RxSharedPreferences` implements `RxStorage`).\n\n```dart\n  Future\u003cvoid\u003e                 update\u003cT extends Object\u003e(String key, Decoder\u003cT?\u003e decoder, Transformer\u003cT?\u003e transformer, Encoder\u003cT?\u003e encoder);\n  Stream\u003cT?\u003e                   observe\u003cT extends Object\u003e(String key, Decoder\u003cT?\u003e decoder);\n  Stream\u003cMap\u003cString, Object?\u003e\u003e observeAll();\n  Future\u003cvoid\u003e                 dispose();\n```\n\n### 5. Get and set methods likes `SharedPreferences`\n\n-   `RxSharedPreferences` is like to `SharedPreferences`, it provides `read`, `write` functions (via extension methods).\n\n```dart\n  Future\u003cObject?\u003e              getObject(String key, [Decoder\u003cObject?\u003e? decoder]);\n  Future\u003cbool?\u003e                getBool(String key);\n  Future\u003cdouble?\u003e              getDouble(String key);\n  Future\u003cint?\u003e                 getInt(String key);\n  Future\u003cSet\u003cString\u003e\u003e          getKeys();\n  Future\u003cString?\u003e              getString(String key);\n  Future\u003cList\u003cString\u003e?\u003e        getStringList(String key);\n\n  Future\u003cMap\u003cString, Object?\u003e\u003e reload();\n  Future\u003cvoid\u003e                 setBool(String key, bool? value);\n  Future\u003cvoid\u003e                 setDouble(String key, double? value);\n  Future\u003cvoid\u003e                 setInt(String key, int? value);\n  Future\u003cvoid\u003e                 setString(String key, String? value);\n  Future\u003cvoid\u003e                 setStringList(String key, List\u003cString\u003e? value);\n```\n\n-   All methods from [Storage](https://pub.dev/documentation/rx_storage/latest/rx_storage/Storage-class.html)\n   (because `RxSharedPreferences` implements `Storage`).\n\n```dart\n  Future\u003cbool\u003e                 containsKey(String key);\n  Future\u003cT?\u003e                   read\u003cT extends Object\u003e(String key, Decoder\u003cT?\u003e decoder);\n  Future\u003cMap\u003cString, Object?\u003e\u003e readAll();\n  Future\u003cvoid\u003e                 clear();\n  Future\u003cvoid\u003e                 remove(String key);\n  Future\u003cvoid\u003e                 write\u003cT extends Object\u003e(String key, T? value, Encoder\u003cT?\u003e encoder);\n```\n\n### 6. Dispose\n\nYou can dispose the `RxSharedPreferences` when it is no longer needed.\nJust call `rxPrefs.dispose()`.\nUsually, you call this method on `dispose` method of a Flutter `State`.\n\n## Example demo\n\n| [Simple authentication app with `BLoC rxdart pattern`](https://github.com/hoc081098/node-auth-flutter-BLoC-pattern-RxDart.git)               | [Build ListView from Stream using `RxSharedPreferences`](https://github.com/hoc081098/rx_shared_preferences/tree/master/example) | [Change theme and locale (language) runtime](https://github.com/hoc081098/bloc_rxdart_playground/tree/master/flutter_change_theme)    |\n|----------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|\n| \u003cimg src=\"https://github.com/hoc081098/node-auth-flutter-BLoC-pattern-RxDart/blob/master/screenshots/Screenshot3.png?raw=true\" height=\"480\"\u003e | \u003cimg src=\"https://github.com/hoc081098/rx_shared_preferences/blob/master/example/example.gif?raw=true\" height=\"480\"\u003e             | \u003cimg src=\"https://github.com/hoc081098/bloc_rxdart_playground/blob/master/flutter_change_theme/Screenshot.gif?raw=true\" height=\"480\"\u003e |\n\n## Features and bugs\n\nPlease file feature requests and bugs at the [issue tracker][tracker].\n\n[tracker]: https://github.com/hoc081098/rx_shared_preferences/issues\n\n## License\n\n```text\nMIT License\n\nCopyright (c) 2019-2024 Petrus Nguyễn Thái Học\n```\n\n## Contributors ✨\n\nThanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://www.linkedin.com/in/hoc081098/\"\u003e\u003cimg src=\"https://avatars.githubusercontent.com/u/36917223?v=4?s=100\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003ePetrus Nguyễn Thái Học\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003cbr /\u003e\u003ca href=\"https://github.com/hoc081098/rx_shared_preferences/commits?author=hoc081098\" title=\"Code\"\u003e💻\u003c/a\u003e \u003ca href=\"https://github.com/hoc081098/rx_shared_preferences/commits?author=hoc081098\" title=\"Documentation\"\u003e📖\u003c/a\u003e \u003ca href=\"#maintenance-hoc081098\" title=\"Maintenance\"\u003e🚧\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-restore --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoc081098%2Frx_shared_preferences","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhoc081098%2Frx_shared_preferences","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhoc081098%2Frx_shared_preferences/lists"}