Prf<T> class

A cached preference object that provides type-safe access to SharedPreferences.

This implementation caches values in memory for faster access after initial read, making it more efficient for repeated access but not suitable for use across isolates. For isolate-safe preferences, use PrfIso instead.

The class supports various types through adapters, including:

  • Basic types (String, int, bool, double, Uint8List & more!)
  • JSON-serializable objects via json factory
  • Enum values via enumerated factory

Example:

// Basic type
final username = Prf<String>('username');
await username.set('Alice');
final name = await username.get(); // Returns 'Alice'

// JSON object
final user = Prf.json<User>(
  'user',
  fromJson: User.fromJson,
  toJson: (user) => user.toJson(),
);

// Enum value
final theme = Prf.enumerated<Theme>(
  'theme',
  values: Theme.values,
  defaultValue: Theme.light,
);
Inheritance
Implementers
Available extensions

Constructors

Prf.new(String key, {T? defaultValue})
Creates a new cached preference object with the given key and optional defaultValue.

Properties

adapter PrfAdapter<T>
The adapter used to convert between the type T and SharedPreferences.
no setteroverride
cachedValue → T?
Returns the currently cached value without accessing SharedPreferences.
no setterinherited
defaultValue → T?
Optional default value to use when no value exists for key.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
isolated PrfIso<T>
Creates an isolate-safe version of this preference.
no setter
key String
The key used to store this preference in SharedPreferences.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

existsOnPrefs() Future<bool>

Available on BasePrfObject<T>, provided by the PrfOperationExtensions extension

Checks if the key exists in the default PrfService instance.
get() Future<T?>

Available on BasePrfObject<T>, provided by the PrfOperationExtensions extension

Gets the value from the default PrfService instance.
getOrFallback(T fallback) Future<T>

Available on BasePrfObject<T>, provided by the PrfOperationExtensions extension

Gets the value or returns the provided fallback if the value is null.
getValue(SharedPreferencesAsync prefs) Future<T?>
Gets the value, using the cached value if available or reading from storage if not.
inherited
init() Future<void>
Initializes the cache by loading the current value from storage.
initCache(SharedPreferencesAsync prefs) Future<void>
Initializes the cache by reading the current value from SharedPreferences.
inherited
isNull() Future<bool>

Available on BasePrfObject<T>, provided by the PrfOperationExtensions extension

Checks if the value is null in the default PrfService instance.
isValueNull(SharedPreferencesAsync prefs) Future<bool>
Checks if the stored value is null.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remove() Future<void>

Available on BasePrfObject<T>, provided by the PrfOperationExtensions extension

Removes the value from the default PrfService instance.
removeValue(SharedPreferencesAsync prefs) Future<void>
Removes the value from SharedPreferences and clears the cached value.
inherited
set(T value) Future<void>

Available on BasePrfObject<T>, provided by the PrfOperationExtensions extension

Sets the value using the default PrfService instance.
setValue(SharedPreferencesAsync prefs, T value) Future<void>
Stores value in SharedPreferences and updates the cached value.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

customAdapter<T>(String key, {required PrfAdapter<T> adapter, T? defaultValue}) Prf<T>
Creates a new preference using a custom adapter.
enumerated<T extends Enum>(String key, {required List<T> values, T? defaultValue}) Prf<T>
Creates a new preference for an enum value.
json<T>(String key, {required T fromJson(Map<String, dynamic> json), required Map<String, dynamic> toJson(T object), T? defaultValue}) Prf<T>
Creates a new preference for a JSON-serializable object.
value<T>(String key, {T? defaultValue}) Future<Prf<T>>
Creates a new pre-cached preference object.