read method
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
Future<PersistedData<String>?> read(String key) async {
final riverpodKey = Helpers.computeRiverpodKey(key);
final encoded = prefs.getString(riverpodKey);
if (encoded == null) return null;
final decoded = jsonDecode(encoded) as Map<String, Object?>;
final mapped = PersistedSpValue.fromJson(decoded);
return PersistedData(
mapped.data,
destroyKey: mapped.destroyKey,
expireAt: mapped.expiresAt,
);
}