Closed Abdktefane closed 1 year ago
Hi! Thanks for asking!
Stock tries to be as simple as possible, so it's defined only by two types, the Key
and the entity type T
:
abstract class Stock<Key, T> {
factory Stock({
required Fetcher<Key, T> fetcher,
required SourceOfTruth<Key, T>? sourceOfTruth,
}) =>
StockImpl<Key, T>(fetcher: fetcher, sourceOfTruth: sourceOfTruth);
On the other hand, as you mentioned sometimes you need to use multiple types, for example, DBEntity
and NetworkEntity
. In this case, what you can do is transform one of them into the other.
You have two ways of doing that:
DBEntity
to the NetworkEntity
. You can do that using the StockTypeMapper
NetworkEntity
to the DBEntity
. This case is the most simple because you have just to map the fetcher response.final fetcher = Fetcher.ofFuture<String, List<NetworkTweet>>(
(userId) => _api.getUserTweets(userId).then((networkResponse) => mapper(networkResponse)), // TODO: implement mapper
);
Maybe we can change the documentation to clarify that. Again, the idea is to use only one type, it doesn't matter what type are you using. There's another complex case. How can I use 3 different types? for that I recommend you to check #28
Thanks for your quick response, I'll create mapper over the Fetcher.
I need to use different entities for Network (
Fetcher
) and DB (SourceOfTruth
), when I'm usingStockTypeMapper
it will convert theSourceOfTruth
entities toFetcher
which is not follow the concept ofSourceOfTruth
. AsStock
documentations says atsource_of_truth.dart
file:For example if I need to observe list of tweets in my UI :
The
stream
response must be same asDB
model notNetwork
model because it's my source of truth. I think we can addStockTypeMapper
to Fetcher class rather thanSourceOfTruth
one, can I achieve this behavior in another way ?