PrfJson<T> class

A cached preference object specialized for storing JSON-serializable objects.

This class provides type-safe access to complex objects stored in SharedPreferences by serializing them to JSON. It uses in-memory caching for improved performance and requires conversion functions to translate between the object type and JSON.

Example:

class User {
  final String name;
  final int age;

  User({required this.name, required this.age});

  factory User.fromJson(Map<String, dynamic> json) => User(
    name: json['name'],
    age: json['age'],
  );

  Map<String, dynamic> toJson() => {
    'name': name,
    'age': age,
  };
}

final userPreference = PrfJson<User>(
  'current_user',
  fromJson: User.fromJson,
  toJson: (user) => user.toJson(),
);

await userPreference.set(User(name: 'Alice', age: 30));
final user = await userPreference.get();
Inheritance
Available extensions

Constructors

PrfJson.new(String key, {required T fromJson(Map<String, dynamic> json), required Map<String, dynamic> toJson(T object), T? defaultValue})
Creates a new cached JSON preference.

Properties

adapter PrfAdapter<T>
Gets the adapter used to convert between the object and its JSON representation.
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
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
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