PrfEncoded<TSource, TStore> constructor

PrfEncoded<TSource, TStore>(
  1. String key, {
  2. required Decode<TSource, TStore> from,
  3. required Encode<TSource, TStore> to,
  4. required Future<TStore?> getter(
    1. SharedPreferencesAsync prefs,
    2. String key
    ),
  5. required Future<void> setter(
    1. SharedPreferencesAsync prefs,
    2. String key,
    3. TStore value
    ),
  6. TSource? defaultValue,
})

Creates a new PrfEncoded variable with the specified encoding and decoding functions.

Parameters:

  • key: Unique identifier for the variable in SharedPreferences
  • from: Function to decode from storage type TStore to source type TSource
  • to: Function to encode from source type TSource to storage type TStore
  • getter: Function to retrieve the encoded value from SharedPreferences
  • setter: Function to save the encoded value to SharedPreferences
  • defaultValue: Optional default value to use when no value is stored

Implementation

PrfEncoded(
  String key, {
  required Decode<TSource, TStore> from,
  required Encode<TSource, TStore> to,
  required Future<TStore?> Function(SharedPreferencesAsync prefs, String key)
      getter,
  required Future<void> Function(
    SharedPreferencesAsync prefs,
    String key,
    TStore value,
  ) setter,
  TSource? defaultValue,
}) : super(
        key,
        (prefs, key) async {
          final stored = await getter(prefs, key);
          return from(stored);
        },
        (prefs, key, value) async {
          final stored = to(value);
          await setter(prefs, key, stored);
        },
        defaultValue,
      );