https://github.com/ciriousjoker/shared_preferences_typed
A type-safe wrapper around shared_preferences, inspired by ts-localstorage.
https://github.com/ciriousjoker/shared_preferences_typed
dart flutter sharedpreferences sharedpreferences-wrapper type-safety
Last synced: 19 days ago
JSON representation
A type-safe wrapper around shared_preferences, inspired by ts-localstorage.
- Host: GitHub
- URL: https://github.com/ciriousjoker/shared_preferences_typed
- Owner: ciriousjoker
- License: bsd-2-clause
- Created: 2022-01-31T08:27:46.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-01T10:50:01.000Z (over 4 years ago)
- Last Synced: 2025-01-29T23:28:47.650Z (over 1 year ago)
- Topics: dart, flutter, sharedpreferences, sharedpreferences-wrapper, type-safety
- Language: Dart
- Homepage: https://pub.dev/packages/shared_preferences_typed
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Typed Shared Preferences
[](https://pub.dartlang.org/packages/shared_preferences_typed)
A type-safe wrapper around [shared_preferences](https://pub.dev/packages/shared_preferences), inspired by [ts-localstorage](https://www.npmjs.com/package/ts-localstorage).
## Why?
- Dart compiler now prevents you from writing a `bool` to an `int` key
- You can organize everything related to [SharedPreferences] in one file, not just the string keys
- You don't need to call `SharedPreferences.getInstance()` anywhere anymore
## Usage
- You create a `PrefKey` or a `PrefKeyNullable`, pass a string as a key and a default value that's returned when the value doesn't exist in the SharedPreferences.
- In case of `PrefKeyNullable`, the default value is still required to guarantee type safety, but it's not actually used anywhere
Check `test/shared_preferences_typed_test.dart` for a more detailed example, here are the most common use cases:
### Basic example (non-nullable)
```dart
/// Description
const key = PrefKey("some_key", true);
final valueBefore = await key.read(); // -> true (default value)
await key.write(false); // -> Value is now false
```
### Basic example (nullable)
```dart
/// Description
const key = PrefKeyNullable("some_key", true);
final valueBefore = await key.read(); // -> null
await key.write(false); // -> Value is now false
```
### Existing SharedPreferences instance (non-nullable)
```dart
final prefs = await SharedPreferences.instance();
/// Description
const key = PrefKey("some_key", true);
final valueBefore = await key.readSync(prefs); // -> true (default value)
await key.write(false); // -> Value is now false
```
### Existing SharedPreferences instance (nullable)
```dart
final prefs = await SharedPreferences.instance();
/// Description
const key = PrefKeyNullable("some_key", true);
final valueBefore = await key.readSync(prefs); // -> null
await key.writeSync(false, prefs); // -> Value is now false
```
## Additional information
- Using an existing SharedPreferences instance has no performance gain if you don't also use it elsewhere. However, the sync methods have benefits when you really can't have `await` somewhere.