Shared preferences typed - A type-safe wrapper around shared preferences, inspired by ts-localstorage

Overview

Typed Shared Preferences

pub package

A type-safe wrapper around shared_preferences, inspired by 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)

/// 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)

/// 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)

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)

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.
You might also like...

A simple dart zeromq implementation/wrapper around the libzmq C++ library

dartzmq A simple dart zeromq implementation/wrapper around the libzmq C++ library Features Currently supported: Creating sockets (pair, pub, sub, req,

Dec 29, 2022

A wrapper around our Cocoa and Java client library SDKs, providing iOS and Android support for those using Flutter and Dart.

A wrapper around our Cocoa and Java client library SDKs, providing iOS and Android support for those using Flutter and Dart.

Ably Flutter Plugin A Flutter plugin wrapping the ably-cocoa (iOS) and ably-java (Android) client library SDKs for Ably, the platform that powers sync

Dec 13, 2022

Easy nav - A simple wrapper around flutter navigator, dialogs and snackbar to do those things without context

EasyNav Just a simple wrapper around flutter navigator, dialogs and snackbar to

Feb 26, 2022

This example uses a ScrollView, JSON Rest API, Navigation, Alert Pop Up, Progress Indicator, Globals, Images in a shared asset folder, and 100% Shared Code

This example uses a ScrollView, JSON Rest API, Navigation, Alert Pop Up, Progress Indicator, Globals, Images in a shared asset folder, and 100% Shared Code

This example uses a ScrollView, JSON Rest API, Navigation, Alert Pop Up, Progress Indicator, Globals, Images in a shared asset folder, and 100% Shared Code. Now with the ability to login with FaceID, TouchID, and Fingerprint Reader on Android.

Jan 6, 2023

SurrealDB client written in pure dart. auto reconnect, typed functions

SurrealDB Client For Dart & Flutter SurrealDB client for Dart and Flutter. Quick Start import 'package:surrealdb/surrealdb.dart'; void main(ListStri

Dec 18, 2022

Lightning fast, strongly typed network protocol

 Lightning fast, strongly typed network protocol

What is Bolt Bolt is a network protocol written in Dart to send and receive strongly typed data objects. It is designed to be easy to use and to be as

Dec 3, 2022

🚗 Apple CarPlay for Flutter Apps. Aims to make it safe to use apps made with Flutter in the car by integrating with CarPlay.

🚗 Apple CarPlay for Flutter Apps. Aims to make it safe to use apps made with Flutter in the car by integrating with CarPlay.

CarPlay with Flutter 🚗 Flutter Apps now on Apple CarPlay! flutter_carplay aims to make it safe to use iPhone apps made with Flutter in the car by int

Dec 26, 2022

A platform to make your social media experience more safe

cyber_watch A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if t

Feb 25, 2022

A web-safe implementation of dart.io.Platforms. Helps avoid the "Unsupported operation: Platform._operatingSystem" runtime error.

Universal Platform - A Web-safe Platform class Currently, if you include the dart.io.Platform anywhere in your code, your app will throw the following

Nov 20, 2022
Flutter-Shared-Preference - The goal is to learn how to use the shared preferences plugin to save important pieces of information to your device.

Recipe Finder The goal is to learn how to use the shared preferences plugin to save important pieces of information to your device. Final App UI Resou

Ashirbad Swain 1 Jan 1, 2022
📦flutter localstorage for ios/android/desktop/web

Localstorage Simple json file-based storage for flutter Installation Add dependency to pubspec.yaml dependencies: ... localstorage: ^4.0.0+1 Run i

Andrei Lesnitsky 267 Dec 16, 2022
Flutter theme demo using riverpod and shared preferences

flutter_theme_demo A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you start

Andree Yosua 0 Dec 27, 2021
Flutter settings manager built on top of Shared Preferences

Settings Manager Flutter settings store built on top of shared preferences. Code Generator for supported types Usage import 'dart:async'; import 'pac

Rody Davis 17 Dec 13, 2022
Flutter Settings Screen with Shared Preferences

Settings Screen with Shared Preferences A library that provides an easy solution to create settings screens with simple and compound options. Features

Barnabás BARTHA 66 Sep 27, 2022
🌀 Shared preferences with RxDart Stream observation

?? Shared preferences with RxDart Stream observation ⚡️ Reactive shared preferences for Flutter ??Reactive stream wrapper around SharedPreferences ?? Lightweight and easy-to-use ?? A reactive key-value store for Flutter projects. Like shared_preferences, but with Streams ?? Rx Shared Preferences for Flutter ?? rx_shared_preferences ?? rx_shared_preference ?? Reactive SharedPreferences for Flutter ?? A stream based wrapper over shared_preferences, allowing reactive key-value storage.

Petrus Nguyễn Thái Học 36 Nov 4, 2022
Shared Preferences ile ekran açılma sayacı uygulaması

sayac_uygulamasi A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started

null 0 Dec 29, 2021
A package for encrypted shared preferences

Prefs Guard Prefs Guard is a data protection wrapper for local storage (Shared Prefs). supporting both IOS & Android. - Notice :- Use Same GuardType t

null 26 Jun 7, 2022
App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and less verbose.

App HTTP Client App HTTP Client is a wrapper around the HTTP library Dio to make network requests and error handling simpler, more predictable, and le

Joanna May 44 Nov 1, 2022
A wrapper around Navigator 2.0 and Router/Pages to make their use a easier.

APS Navigator - App Pagination System This library is just a wrapper around Navigator 2.0 and Router/Pages API that tries to make their use easier: ??

Guilherme Silva 14 Oct 17, 2022