SourceOfTruth, as its name implies, is the persistence API which Stock uses to serve values to the collectors. If provided, Stock will only return values received from SourceOfTruth back to the collectors.
In other words, values coming from the Fetcher will always be sent to the SourceOfTruth and will be read back via reader to then be returned to the collector.
This round-trip ensures the data is consistent across the application in case the Fetcher does not return all fields or returns a different class type than the app uses. It is particularly useful if your application has a local observable database which is directly modified by the app, as Stock can observe these changes and update the collectors even before value is synced to the backend.
SourceOfTruth takes care of making any source (no matter if it has flowing reads or not) into a common flowing API.
A source of truth is usually backed by local storage. Its purpose is to eliminate the need for waiting on a network update before local modifications are available (via Stock.stream).
For maximal simplicity, write's record type (T
] and reader's record
type (T
) are identical. However, sometimes reading one type of objects
from network and transforming them to another type when placing them in
local storage is needed.
For this case you can use the mapTo
and mapToUsingMapper
extensions.
- Implementers
- Available Extensions
Constructors
-
SourceOfTruth({required Stream<
T?> reader(Key key), required Future<void> writer(Key key, T? output), Future<void> delete(Key key)?, Future<void> deleteAll()?}) -
Creates a source of truth that is accessed via
reader
,writer
,delete
anddeleteAll
.factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
delete(
Key key) → Future< void> -
Used by Stock to delete records in the source of truth for
the given
key
. -
deleteAll(
) → Future< void> - Used by Stock to delete all records in the source of truth.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reader(
Key key) → Stream< T?> -
Used by Stock to read records from the source of truth for the given
key
. -
toString(
) → String -
A string representation of this object.
inherited
-
write(
Key key, T? value) → Future< void> - Used by Stock to write records coming in from the fetcher (network) to the source of truth.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited