rx_shared_preference 1.0.1 rx_shared_preference: ^1.0.1 copied to clipboard
Shared preference with RxDart Stream observation. Reactive shared preferences for Flutter. Reactive stream wrapper around SharedPreferences. Lightweight and easy-to-use.
rx_shared_preference #
Author: Petrus Nguyễn Thái Học #
- 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 return Observable
#
Observable
will emit the initial value as its first next event (Emit value as soon as possible after is listen to) (emitnull
when value is not set)- It will automatic emit value when value associated with key was changed successfully (emit
null
when value associated with key wasremoved
or set tonull
) - When read value is not valid type (wrong type):
- Will emit error if value is present (not
null
) - Emit
null
when value is absent (value is not set, value isnull
) (becausenull
can be cast to any type).
- Will emit error if value is present (not
- Can emit two consecutive data events that are equal. You should use Rx operator like
distinct
(More commonly known asdistinctUntilChanged
in other Rx implementations) to create anObservable
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
dependencies:
...
rx_shared_preference: <latest_version>
Usage #
Import rx_shared_preference
and shared_preferences
import 'package:rx_shared_preference/rx_shared_preference.dart';
import 'package:shared_preferences/shared_preferences.dart';
Wrap your SharedPreferences
in a RxSharedPreferences
.
final rxSharedPreferences = RxSharedPreferences(
await SharedPreferences.getInstance()
);
or not need keyword await
before SharedPreferences.getInstance()
final rxSharedPreferences = RxSharedPreferences(
SharedPreferences.getInstance()
);
You can add logger optional parameter to RxSharedPreferences
constructor.
Logger will log messages about operations (such as read, write) and observable values
final rxSharedPreferences = RxSharedPreferences(
SharedPreferences.getInstance(),
(message) => print('[RX_SHARED_PREF] :: $message'),
);
or simply pass print
function to RxSharedPreferences
constructor
final rxSharedPreferences = RxSharedPreferences(
SharedPreferences.getInstance(),
print,
);
And then, just listen Observable
, transform Observable
through operators such as (map
, flatMap
, etc...).
If you need listen to this Observable
many times, you can use broadcast operators such as share
, shareValue
, publish
, publishValue
, ...
rxSharedPreferences.getStringListObservable('KEY').listen(print);
rxSharedPreferences.getStringListObservable('KEY').share();
rxSharedPreferences.getStringListObservable('KEY').shareValue();
rxSharedPreferences.getIntObservable('KEY')
.map((i) => /*Do something cool*/)
...
...
RxSharedPreferences
is like to SharedPreferences
, it provides read write functions: getBool
, getDouble
, getInt
, ..., setBool
, setDouble
, setInt
, etc...
Example demo: #
Build ListView from Stream using RxSharedPreferences #
Demo | Code |
---|---|
Change theme runtime: #
https://github.com/hoc081098/flutter-change-theme-youtube-code
License #
MIT License
Copyright (c) 2019 Petrus Nguyễn Thái Học
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.