getCustomValue<T> method

Preference<T> getCustomValue<T>(
  1. String key, {
  2. required T defaultValue,
  3. required PreferenceAdapter<T> adapter,
})

Creates a Preference with a custom type. Requires an implementation of a PreferenceAdapter.

Like all other "get()" methods, starts with a current value for the given key, then emits a new value every time there are changes to the value associated with key.

Uses an adapter for storing and retrieving the custom type from the persistent storage. For an example of a custom adapter, see the source code for getString and StringAdapter.

If the value is null, starts with the value provided in defaultValue. When the value transitions from non-null to null (ie. when the value is removed), emits defaultValue.

Implementation

Preference<T> getCustomValue<T>(
  String key, {
  required T defaultValue,
  required PreferenceAdapter<T> adapter,
}) {
  assert(
    // ignore: invalid_use_of_internal_member
    key != Preference.$$_getKeysKey || adapter is _GetKeysAdapter,
    // ignore: invalid_use_of_internal_member
    '"${Preference.$$_getKeysKey}" is a reserved key.',
  );

  return _getValue(
    key,
    defaultValue: defaultValue,
    adapter: adapter,
  );
}