CustomHttpRepository<Data> class
abstract
A CustomHttpRepository is a Repository that fetches data from an Http
endpoint. It differs from HttpRepository
in that it allows you to
customize the request.
- Inheritance
-
- Object
- Repository<
Data> - CustomHttpRepository
- Implementers
Constructors
-
CustomHttpRepository({Duration? autoRefreshInterval, bool resolveOnCreate = true, String? tag, Future<
String> accessTokenBuilder()?}) -
A CustomHttpRepository is a Repository that fetches data from an Http
endpoint. It differs from
HttpRepository
in that it allows you to customize the request.
Properties
-
accessTokenBuilder
↔ (Future<
String> Function()?) -
A callback that returns the access token.
getter/setter pair
- 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.
finalinherited
-
currentState
→ RepositoryState<
Data> -
Returns the current state of the repository.
no setterinherited
- currentValue → Data?
-
Getter for the last value of the stream.
Returns null if the stream is empty.
no setterinherited
- endpoint → Uri
-
The endpoint to fetch data from.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
-
hydratationFiber
→ RepositoryFiber<
Data?> -
Fiber used to hydrate the repository.
finalinherited
- 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 setteroverride
- name → String
-
Repository name used in logs and debugging.
no setteroverride
-
refreshFiber
→ RepositoryFiber<
Data> -
The
Fiber
is used to avoid multiple refreshes at the same time.finalinherited - 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.
latefinalinherited
- tag → String?
-
The tag of the repository.
This is used to identify the repository in the cache.
If the tag is not null, the repository will use the tag to
create unique keys for the cache.
A commom use case for this is when you want to cache for different
users. In this case, you can use the user id (e.g. e-mail) as the tag.
final
- timer ↔ Timer?
-
Internal timer used to refresh the repository.
getter/setter pairinherited
Methods
-
clear(
) → Future< void> -
Clears the cache and emits an empty state to the repository stream.
inherited
-
clearCache(
) → Future< void> -
Clears the cache.
inherited
-
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.
inherited
-
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.
inherited
-
emit(
{required Data data, RepositoryDatasource datasource = RepositoryDatasource.local}) → Future< void> -
Emits a new data to the repository.
inherited
-
fromJson(
String json) → Data -
Transforms the raw data from the remote source to the data that will be
used in the stream.
inherited
-
hydrate(
{bool refreshAfter = true}) → Future< Data?> -
Gets the data from the cache, if it exists, and emits it to the stream.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onErrorStatusCode(
int statusCode) → Future< void> - Called when the endpoint returns a unsuccessful status code. See also successfulCondition.
-
onSocketException(
Exception exception) → FutureOr< void> - If the request fails with SocketException, this callback will be called with the repository itself as an argument. This callback is useful for retrying the request.
-
refresh(
) → Future< Data> -
Refreshes the repository from remote datasource.
inherited
-
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.
override
-
shouldRetry(
Exception exception) → FutureOr< bool> -
Used to decide if the repository should retry after an error.
override
-
successfulCondition(
int statusCode, dynamic body) → bool -
Condition to determine if the endpoint returns a successful status code.
The default condition is
statusCode >= 200 && statusCode < 300
. If you want to change the condition, override this method. -
toString(
) → String -
A string representation of this object.
inherited
-
track(
) → void -
Method used to add this repository to the pool.
It's useful for debugging.
inherited
-
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.inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- client → const HttpRepositoryHttpClient
-
The http client used to fetch data from the endpoint.
This is a monostate, so it will be shared across all instances of
CustomHttpRepository and
HttpRepository
.