PrfIso<T> class
An isolate-safe preference object that provides type-safe access to SharedPreferences.
Unlike Prf, this implementation does not cache values in memory, making it suitable for use across isolates but requiring disk reads for each access. For non-isolate-safe preferences with caching, use Prf 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
You can also create an isolate-safe preference from an existing Prf instance
using the .isolated
getter:
// Create a cached preference and then get an isolate-safe version
final isolatedUsername = Prf<String>('username').isolated;
await isolatedUsername.set('Alice');
final name = await isolatedUsername.get();
Example:
// Basic type
final username = PrfIso<String>('username');
await username.set('Alice');
final name = await username.get(); // Always reads from disk
// JSON object
final user = PrfIso.json<User>(
'user',
fromJson: User.fromJson,
toJson: (user) => user.toJson(),
);
// Enum value
final theme = PrfIso.enumerated<Theme>(
'theme',
values: Theme.values,
defaultValue: Theme.light,
);
- Inheritance
-
- Object
- BasePrfObject<
T> - PrfIso
- Available extensions
Constructors
- PrfIso.new(String key, {T? defaultValue})
-
Creates a new isolate-safe preference object with the given
key
and optionaldefaultValue
.
Properties
-
adapter
→ PrfAdapter<
T> -
The adapter used to convert between the type
T
and SharedPreferences.no setteroverride - 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<
Checks if the key exists in the default PrfService instance.T> , provided by the PrfOperationExtensions extension -
get(
) → Future< T?> -
Available on BasePrfObject<
Gets the value from the default PrfService instance.T> , provided by the PrfOperationExtensions extension -
getOrFallback(
T fallback) → Future< T> -
Available on BasePrfObject<
Gets the value or returns the provided fallback if the value is null.T> , provided by the PrfOperationExtensions extension -
getValue(
SharedPreferencesAsync prefs) → Future< T?> -
Gets the value from SharedPreferences.
inherited
-
isNull(
) → Future< bool> -
Available on BasePrfObject<
Checks if the value is null in the default PrfService instance.T> , provided by the PrfOperationExtensions extension -
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<
Removes the value from the default PrfService instance.T> , provided by the PrfOperationExtensions extension -
removeValue(
SharedPreferencesAsync prefs) → Future< void> -
Removes the value from SharedPreferences.
inherited
-
set(
T value) → Future< void> -
Available on BasePrfObject<
Sets the value using the default PrfService instance.T> , provided by the PrfOperationExtensions extension -
setValue(
SharedPreferencesAsync prefs, T value) → Future< void> -
Stores
value
in SharedPreferences.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}) → PrfIso<T> - Creates a new preference using a custom adapter.
-
enumerated<
T extends Enum> (String key, {required List< T> values, T? defaultValue}) → PrfIso<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}) → PrfIso<T> - Creates a new preference for a JSON-serializable object.