read method

  1. @override
FutureOr<PersistedData<String>?> read(
  1. String key
)

Reads the data associated with key.

If no data is associated with key, this method should return null. Otherwise it should return the data associated with key.

It is fine to return expired data, as NotifierPersistX.persist will handle the expiration logic.

If possible, make this method synchronous. This can enable NotifierPersistX.persist to be synchronous too ; which will allow the persisted data to be available as soon as possible in the UI.

Implementation

@override
FutureOr<PersistedData<String>?> read(String key) {
  final value = _preferences.getString("$_prefix:$key");
  final destroyKey = _preferences.getString("$_prefix:$key:destroy");
  final expireAtString = _preferences.getString(
    "$_prefix:$key:expire_at",
  );

  if (value == null) return null;

  final expireAt = DateTime.tryParse(expireAtString ?? '');

  return PersistedData(
    value,
    destroyKey: destroyKey,
    expireAt: expireAt,
  );
}