PrfJson<T> class

A type-safe wrapper for storing and retrieving JSON-serializable objects in SharedPreferences.

This class automatically handles the conversion between Dart objects and their JSON string representation for storage in SharedPreferences.

Use this class for storing complex objects like user profiles, app configurations, or any other structured data that can be serialized to 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 currentUser = PrfJson<User>(
  'current_user',
  fromJson: User.fromJson,
  toJson: (user) => user.toJson(),
);

await currentUser.set(User(name: 'Joey', age: 30));
final user = await currentUser.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 JSON preference variable with the specified key.

Properties

defaultValue → T?
Default value to use when no value is stored yet.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
key String
Unique key used to store this variable in SharedPreferences.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

existsOnPrefs() Future<bool>

Available on PrfVariable<T>, provided by the PrfVariableExtensions extension

Checks if the key exists in SharedPreferences.
get() Future<T?>

Available on PrfVariable<T>, provided by the PrfVariableExtensions extension

Gets the stored value associated with this variable.
getOrFallback(T fallback) Future<T>

Available on PrfVariable<T>, provided by the PrfVariableExtensions extension

Gets the stored value or returns a fallback if the value is null.
getValue(SharedPreferencesAsync prefs) Future<T?>
Retrieves the current value from cache or SharedPreferences.
inherited
isNull() Future<bool>

Available on PrfVariable<T>, provided by the PrfVariableExtensions extension

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

Available on PrfVariable<T>, provided by the PrfVariableExtensions extension

Removes the value from persistent storage.
removeValue(SharedPreferencesAsync prefs) Future<void>
Removes the value from both the cache and SharedPreferences.
inherited
set(T value) Future<void>

Available on PrfVariable<T>, provided by the PrfVariableExtensions extension

Sets the value for this variable in persistent storage.
setValue(SharedPreferencesAsync prefs, T value) Future<void>
Saves a new value to SharedPreferences and updates the cache.
inherited
toString() String
A string representation of this object.
inherited

Operators

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