Coder Social home page Coder Social logo

roundedinfinity / clever_settings Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 44 KB

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

License: MIT License

Dart 100.00%
dart dart-package settings settings-storage

clever_settings's Introduction

Clever Settings

style: very good analysis Powered by Mason License: MIT Coverage

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.

clever_settings's People

Contributors

roundedinfinity avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.