clever_settings 0.1.2+1 clever_settings: ^0.1.2+1 copied to clipboard
Clever Settings is a Dart package that provides an easy way to manage and store settings for your application.
Clever Settings #
Clever Settings is a Dart package that provides an easy way to manage and store settings for your application. It uses Hive, a lightweight and fast key-value store, to persist the settings data.
Installation 💻 #
Add the following to your pubspec.yaml
:
dependencies:
clever_settings: ^[version]
# When using flutter (optional)
clever_settings_flutter: ^[version]
Install it:
dart pub get
Initialization 🚀 #
Before you can start using Clever Settings, you need to initialize Hive first by calling Hive.init
(or Hive.initFlutter
). Call the open function once at the start of your application:
// Initialize Hive before this
await CleverSettings.open();
Alternatively, when using clever_settings_flutter, you can just call:
await CleverSettingsFlutter.init();
Usage 💡 #
Clever Settings provides multiple classes for managing settings. SettingsValue is used for storing primitive types like bool, int, String, etc. SerializableSettingsValue is used for storing complex objects that can be serialized to and from JSON.
For additional flutter features, see clever_settings_flutter.
Example #
class Settings {
const Settings._();
/// A settings value that uses a bool value
static const darkMode =
SettingsValue<bool>(name: 'darkMode', defaultValue: false);
}
...
void myMethod() {
// This is automatically saved to disk.
Settings.darkMode.value = true;
// Get the value.
final darkModeEnabled = Settings.darkMode.value;
}
SettingsValue #
To create a new SettingsValue
, simply provide a unique name for the setting and an optional default value:
final mySetting = SettingsValue<bool>(name: 'mySetting', defaultValue: true);
You can then get or set the value of the setting. This automatically stores the setting to storage:
final currentValue = mySetting.value;
mySetting.value = false;
You can also listen to changes to the setting by calling the watch
function:
final stream = mySetting.watch();
stream.listen((newValue) {
// do something with the new value
});
SerializableSettingsValue #
SerializableSettingsValue
works the same way as SettingsValue
, but you also need to provide fromJson
and toJson
functions to serialize and deserialize the value:
final myObjectSetting = SerializableSettingsValue<MyObject>(
name: 'myObjectSetting',
fromJson: (json) => MyObject.fromJson(json),
toJson: (object) => object.toJson(),
);
DefaultSettingsValue #
DefaultSettingsValue is a subclass of SettingsValue that provides a default value if no value is specified by the user. The value stored in this class is guaranteed to be non-null.
final setting = SettingsValue<int>(name: 'setting', defaultValue: 34);
final defaultSetting = DefaultSettingsValue<int>(name: 'default', defaultValue: 16);
// This value is of type int? even though it has a default value.
final value = setting.value;
// This value is of type int and not nullable.
final otherValue = defaultSetting.value;
License #
This package is released under the MIT License. See LICENSE for more information.