Repository<Data> class
abstract
A Repository is a class that holds data and provides a stream. It can be used to fetch data from a remote source, cache it, and provide a stream of that data.
- Implementers
Constructors
- Repository({Duration? autoRefreshInterval, bool resolveOnCreate = true})
-
If
resolveOnCreate
is true, the repository will resolve itself on creation. If autoRefreshInterval is not null, the repository will refresh itself every autoRefreshInterval. -
Repository.http({required Uri endpoint, Data fromJson(String json)?, FutureOr<
bool> shouldRetryCondition(Exception exception)?, Duration? autoRefreshInterval, String? tag, bool resolveOnCreate = true, String? name}) -
Exception thrown when the endpoint returns a unsuccessful status code.
factory
Properties
- autoRefreshInterval → Duration?
-
The interval at which the repository will refresh itself.
If null, the repository will not refresh itself.
If not null, the repository will refresh itself
every autoRefreshInterval.
final
-
currentState
→ RepositoryState<
Data> -
Returns the current state of the repository.
no setter
- currentValue → Data?
-
Getter for the last value of the stream.
Returns null if the stream is empty.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
hydratationFiber
→ RepositoryFiber<
Data?> -
Fiber used to hydrate the repository.
final
- key → String
-
The key used to save the data in the cache.
This key must be unique.
If you have multiple repositories with the same key, the cache will be
overwritten.
no setter
- name → String
-
Repository name used in logs and debugging.
no setter
-
refreshFiber
→ RepositoryFiber<
Data> -
The
Fiber
is used to avoid multiple refreshes at the same time.final - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
stream
→ Stream<
RepositoryState< Data> > -
The stream of the repository.
This stream will emit the data every time it changes.
The data will be cached locally.
The data will be refreshed every autoRefreshInterval.
The data will be refreshed when refresh is called.
latefinal
- tag → String?
-
The tag used to differ cache from same key repositories. It's commonly
used to build the
key
property.no setter - timer ↔ Timer?
-
Internal timer used to refresh the repository.
getter/setter pair
Methods
-
clear(
) → Future< void> - Clears the cache and emits an empty state to the repository stream.
-
clearCache(
) → Future< void> - Clears the cache.
-
currentValueOrResolve(
) → Future< Data> - Get the current data of the repository if it's already resolved, otherwise resolve it. This method will not refresh the repository if it's already resolved.
-
dispose(
) → void - Disposes the repository. You should call this method when you're done using the repository. This method will cancel the timer and close the stream. You should not use the repository after calling this method.
-
emit(
{required Data data, RepositoryDatasource datasource = RepositoryDatasource.local}) → Future< void> - Emits a new data to the repository.
-
fromJson(
String json) → Data - Transforms the raw data from the remote source to the data that will be used in the stream.
-
hydrate(
{bool refreshAfter = true}) → Future< Data?> - Gets the data from the cache, if it exists, and emits it to the stream.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
refresh(
) → Future< Data> - Refreshes the repository from remote datasource.
-
resolve(
) → Future< String> - Gets the data from the remote source and returns the raw data. The returned string will be saved in the cache and decoded using fromJson.
-
shouldRetry(
Exception exception) → FutureOr< bool> - Used to decide if the repository should retry after an error.
-
toString(
) → String -
A string representation of this object.
override
-
track(
) → void - Method used to add this repository to the pool. It's useful for debugging.
-
update(
Data resolver(Data? data)) → Future< void> -
Updates the current data and refresh the repository.
The
resolver
function takes the current data and returns the new data. The new data will be added to the stream and the repository will be refreshed. This method is useful if you want to use Optimistic UI. You can update the data to the repository and refresh in a row.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Properties
- logger ↔ RepositoryLogger
-
Monostate logger service to log messages.
getter/setter pair
-
repositories
→ List<
WeakReference< Repository> > -
Pool repositories that are tracked.
final
- storage ↔ RepositoryCacheStorage
-
Monostate cache service to save data locally. It should be initialized
before using any repository.
getter/setter pair