Clever Settings

style: very good analysis Powered by Mason License: MIT

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 💻

❗ In order to start using Clever Settings you must have the Dart SDK installed on your machine.

Add clever_settings to your pubspec.yaml:

dependencies:
  clever_settings:

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();

Usage

Clever Settings provides two classes for managing settings: SettingsValue and SerializableSettingsValue. 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.

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(),
);

License

This package is released under the MIT License. See LICENSE for more information.

Libraries

clever_settings
Clever Settings is a Dart package that provides an easy way to manage and store settings for your application.