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
-
- Object
- PrfVariable<
T> - PrfEncoded<
T, String> - PrfJson
- Available extensions
Constructors
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<
Checks if the key exists in SharedPreferences.T> , provided by the PrfVariableExtensions extension -
get(
) → Future< T?> -
Available on PrfVariable<
Gets the stored value associated with this variable.T> , provided by the PrfVariableExtensions extension -
getOrFallback(
T fallback) → Future< T> -
Available on PrfVariable<
Gets the stored value or returns a fallback if the value is null.T> , provided by the PrfVariableExtensions extension -
getValue(
SharedPreferencesAsync prefs) → Future< T?> -
Retrieves the current value from cache or SharedPreferences.
inherited
-
isNull(
) → Future< bool> -
Available on PrfVariable<
Checks if the current value is null in storage.T> , provided by the PrfVariableExtensions extension -
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<
Removes the value from persistent storage.T> , provided by the PrfVariableExtensions extension -
removeValue(
SharedPreferencesAsync prefs) → Future< void> -
Removes the value from both the cache and SharedPreferences.
inherited
-
set(
T value) → Future< void> -
Available on PrfVariable<
Sets the value for this variable in persistent storage.T> , provided by the PrfVariableExtensions extension -
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