Last updated:
0 purchases
extended shared preferences
Extended SharedPreferences #
This is an extension of the shared_preferences plugin so that the function of multiple SharedPreference files is now supported in iOS and in Android.
This plugin wraps NSUserDefaults (on iOS) and SharedPreferences (on Android), providing a persistent store for simple data. Data is persisted to disk asynchronously. Neither platform can guarantee that writes will be persisted to disk after returning and this plugin must not be used for storing critical data.
The deprecated commit() function is removed.
Getting Started #
Implement this plugin in your flutter project and use it with SharedPreferences.standard or SharedPreferences() to use the default preference store or use your own one with SharedPreferences("[Your preference file]").
Before you can use the preference store you must call await SharedPreferences().loadPersistentStore() to load the preferences from the storage.
You can read values by calling
/// Read a bool value.
var v = SharedPreferences().getBool([Your Key]);
/// Read an int value.
var v = SharedPreferences().getInt([Your Key]);
/// Read a double value.
var v = SharedPreferences().getDouble([Your Key]);
/// Read a string value.
var v = SharedPreferences().getString([Your Key]);
/// Read a string list value.
var v = SharedPreferences().getStringList([Your Key]);
/// Read any value.
var v = SharedPreferences().get([Your Key]);
copied to clipboard
Write values by calling
/// Write a bool value.
SharedPreferences().setBool([Your Key], [Bool Value]);
/// Write an int value.
SharedPreferences().setInt([Your Key], [Int Value]);
/// Write a double value.
SharedPreferences().setDouble([Your Key], [Double Value]);
/// Write a string value.
SharedPreferences().setString([Your Key], [String Value]);
/// Write a string list.
SharedPreferences().setStringList([Your Key], [String List]);
/// Write any value.
SharedPreferences().set([Your Key], [Value]);
copied to clipboard
You can check if a key exists by calling
SharedPreferences().containsKey([Your Key]);
copied to clipboard
You get a key set of all keys of the current file by calling
var v = SharedPreferences().getKeys();
copied to clipboard
You can reload the values from the disk manually by calling
SharedPreferences().reload();
copied to clipboard
Values can be removed by calling
SharedPreferences().remove([Your Key]);
copied to clipboard
or
SharedPreferences().clear();
copied to clipboard
if you want to clear all values of the preference file.
Usage #
To use this plugin, add extended_shared_preferences as a dependency in your pubspec.yaml file.
Example #
class AudioLevel extends StatefulWidget {
@override
_AudioLevelState createState() => _AudioLevelState();
}
class _AudioLevelState extends State<AudioLevel> {
int audioLevel;
final sharedPreferences = SharedPreferences("audioSettings");
@override
void initState() {
audioLevel = sharedPreferences.getInt("audioLevel");
super.initState();
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
Text("Current Level:"),
Row(
children: <Widget>[
FlatButton(
child: Icon(Icons.remove),
onPressed: () => setState(() {
audioLevel--;
sharedPreferences.setInt("audioLevel", audioLevel);
}),
),
Text("$audioLevel"),
FlatButton(
child: Icon(Icons.add),
onPressed: () => setState(() {
audioLevel++;
sharedPreferences.setInt("audioLevel", audioLevel);
}),
),
],
),
],
)
);
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.