settings_builder

Last updated:

0 purchases

settings_builder Image
settings_builder Images
Add to Cart

Description:

settings builder

settings_builder #


A dart builder that generates automatic accessors for reading and writing shared preferences.
Table of contents #

Features
Installation
Usage
Documentation

Table of contents generated with markdown-toc
Features #

Generates compile time safe access to SharedPreferences.
Assumes a native API like shared_preferences

Does not depend on that package, which means you can implement your own if whished.
Works with non flutter projects


Built-In support for all types compatible with SharedPreferences

Allow definition of default values
Supports any type via custom converters.


Hierarchical structuring

Installation #
As this is a builder package, you need to also install the annotations and build_runner:
dependencies:
settings_annotation: <latest>

dev_dependencies:
build_runner: <latest>
settings_builder: <latest>
copied to clipboard
Usage #
The following is a very basic example to get started. For a more feature complete example, check out the Example page.
Generating the settings does not required much setup. The following is a very basic example to get things running. It
provides a simple settings instance with two entries: a logLevel, stored as an int, and an account. The account
itself is a subgroup that contains a loggedIn boolean which defaults to false and a name.
part 'settings.g.dart'; // assume this file is named 'settings.dart'

@SettingsGroup()
abstract class AccountSettings with _$AccountSettings {
@SettingsEntry(defaultValue: false)
bool get loggedIn;

String? get name;
}

@SettingsGroup(root: true)
abstract class Settings with _$Settings {
factory Settings(SharedPreferences sharedPreferences, [String? prefix]) =
_$SettingsImpl;

static Future<Settings> getInstance([String? prefix]) =>
_$SettingsImpl.getInstance();

AccountSettings get account;

int? get logLevel;
}
copied to clipboard
Now run dart run build_runner build and the code will get generated.
To use these generated settings, you can simply create a new instance via getInstance and then use the getters and
the other generated members:
final settings = await Settings.getInstance();

// the keys under which settings are stored are auto generated:
print(settings.account.loggedInKey); // prints 'account.loggedIn'
print(settings.logLevelKey); // prints 'logLevel'

// initially, values are not set:
print(settings.account.hasLoggedIn); // prints false
print(settings.account.loggedIn); // prints false
print(settings.hasLogLevel); // prints false
print(settings.logLevel); // prints null

// you can update them:
await settings.setLogLevel(10);
print(settings.hasLogLevel); // prints true
print(settings.logLevel); // prints 10

// and you can remove them again
await settings.removeLogLevel();
print(settings.hasLogLevel); // prints false
print(settings.logLevel); // prints null
copied to clipboard
Documentation #
The documentation is available on pub.dev:

settings_annotation
settings_builder

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.