rx_shared_preferences 1.1.1+1

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

rx_shared_preferences alt text #

Author: Petrus Nguyễn Thái Học #

Codacy Badge Pub codecov Build Status License: MIT

  • Shared preference with rxdart Stream observation.
  • Reactive shared preferences for Flutter.
  • Reactive stream wrapper around SharedPreferences.
  • This package provides reactive shared preferences interaction with very little code. It is designed specifically to be used with Flutter and Dart.

More detail about returned Stream #

  • Stream will emit the initial value as its first next event when it is listen to (emit null when value is not set).

  • It will automatic emits value when value associated with key was changed successfully (emit null when value associated with key was removed or set to null).

  • When read value is invalid type (ie. wrong type):

    • Will emit error if value is present (ie. not null).
    • Emit null when value is not set (ie. value is null) (this occurred because null can be cast to any type).
    • Can emit two consecutive data events that are equal. You should use Rx operator like distinct (More commonly known as distinctUntilChanged in other Rx implementations) to create an Stream where data events are skipped if they are equal to the previous data event.

Getting Started #

In your flutter project, add the dependency to your pubspec.yaml

  rx_shared_preferences: ^1.1.0

Usage #

1. Import and instance #

Import rx_shared_preferences and shared_preferences

import 'package:rx_shared_preferences/rx_shared_preferences.dart';
import 'package:shared_preferences/shared_preferences.dart';

Wrap your SharedPreferences in a RxSharedPreferences.

final rxPrefs = RxSharedPreferences(await SharedPreferences.getInstance());
final rxPrefs = RxSharedPreferences(SharedPreferences.getInstance()); // await is optional
final rxPrefs = RxSharedPreferences.getInstance(); // default singleton instance

2. Can add a logger #

You can add logger optional parameter to RxSharedPreferences constructor. Logger will log messages about operations (such as read, write) and stream events

final rxPrefs = RxSharedPreferences(
  const DefaultLogger(),

You can custom Logger by implements Logger, or extends class LoggerAdapter (with empty implementations)

class MyLogger extends LoggerAdapter {
  const MyLogger();

  void readValue(Type type, String key, value) {
    // do something

final rxPrefs = RxSharedPreferences(
  const MyLogger(),

3. Select stream and use #

  • And then, just listen Stream, transform Stream through operators such as (map, flatMap, etc...).
  • If you need listen to this Stream many times, you can use broadcast operators such as share, shareValue, publish, publishValue, ...
// Listen
rxPrefs.getStringListStream('KEY_LIST').listen(print); // [*]

// Broadcast stream

// Transform stream
  .map((i) => /* Do something cool */)
  .where((i) => /* Filtering */)

// must **use same rxPrefs** instance when set value and select stream
rxPrefs.setStringList('KEY_LIST', ['Cool']); // [*] will print ['Cool']

  • In the previous example we re-used the RxSharedPreferences object rxPrefs for set operations. All set operations must go through this object in order to correctly notify subscribers.

  • In flutter, you:

    • Can create global RxSharedPreferences instance.
    • Using singleton instance RxSharedPreferences.getInstance()
    • Can use InheritedWidget/Provider to provide a RxSharedPreferences instance (create it in main function) for all widgets (recommended). See example/main, example/provider.
rxPrefs1.getStringListStream('KEY_LIST').listen(print); // [*]

rxPrefs2.setStringList('KEY_LIST', ['Cool']); // [*] will not print anything

The previous example is wrong usage.

4. Get and set methods like to SharedPreferences #

RxSharedPreferences is like to SharedPreferences, it provides read, write functions:

-   Future<bool> containsKey(String key);
-   Future<dynamic> get(String key);
-   Future<bool> getBool(String key);
-   Future<double> getDouble(String key);
-   Future<int> getInt(String key);
-   Future<Set<String>> getKeys();
-   Future<String> getString(String key);
-   Future<List<String>> getStringList(String key);

-   Future<bool> clear();
-   Future<void> reload();
-   Future<bool> commit();
-   Future<bool> remove(String key);
-   Future<bool> setBool(String key, bool value);
-   Future<bool> setDouble(String key, double value);
-   Future<bool> setInt(String key, int value);
-   Future<bool> setString(String key, String value);
-   Future<bool> setStringList(String key, List<String> value);

5. Dispose RxSharedPreferences #

You can dispose RxSharedPreferences when is no longer needed. Just call rxPrefs.dispose(). Usually you call this method on dispose of a State

Example demo #

Simple authentication app with BLoC rxdart patternBuild ListView from Stream using RxSharedPreferencesChange theme and locale (language) runtime

License #

Copyright (c) 2019 Petrus Nguyễn Thái Học

1.1.1+1 #

  • Update description in pubspec.yaml

1.1.1 - Jan 29, 2020 #

  • Add getKeysStream method to IRxSharedPreferences.
  • Add constructor RxSharedPreferences.getInstance() that returns default singleton RxSharedPreferences instance.
  • Internal implementation refactor & fix default logger.

1.1.0 - Dec 18, 2019 #

  • Update dependencies.
  • Now IRxSharedPreferences's methods return Stream instead of Observable.

1.0.3+2 - Oct 7, 2019 #

  • Update dependencies.

1.0.3+1 - Aug 9, 2019 #

  • Update README.md.

1.0.3 - Aug 9, 2019 #


import 'package:example/home.dart';
import 'package:example/loggers.dart';
import 'package:example/rx_prefs_provider.dart';
import 'package:flutter/material.dart';
import 'package:rx_shared_preferences/rx_shared_preferences.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {

  /// Singleton instance for app
  final rxPrefs = RxSharedPreferences(
      rxPrefs: rxPrefs,
      child: MyApp(),

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Rx Shared Preferences example',
      theme: ThemeData.dark(),
      home: const MyHomePage(),

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  rx_shared_preferences: ^1.1.1+1

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:rx_shared_preferences/rx_shared_preferences.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
flutter 0.0.0
rxdart ^0.23.1 0.23.1 0.24.0-dev.1
shared_preferences ^0.5.6 0.5.6+3
Transitive dependencies
flutter_web_plugins 0.0.0
meta 1.1.8
shared_preferences_macos 0.0.1+6
shared_preferences_web 0.1.2+4
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
collection ^1.14.11 1.14.11 1.14.12
pedantic ^1.8.0
shared_preferences_platform_interface ^1.0.1 1.0.3
test_api ^0.2.11