HttpRepository<Data> class
Exception thrown when the endpoint returns a unsuccessful status code.
- Inheritance
-
- Object
- Repository<
Data> - CustomHttpRepository<
Data> - HttpRepository
Constructors
-
HttpRepository({required Uri endpoint, Data fromJson(String json)?, FutureOr<
bool> shouldRetryCondition(Exception exception)?, bool resolveOnCreate = true, Duration? autoRefreshInterval, String? tag, String? name}) -
Creates a HttpRepository that fetches data from an endpoint.
The endpoint is the only required parameter.
The
fromJson
function is optional and defaults to returning the json as is. TheautoRefreshInterval
is optional and defaults to null. TheresolveOnCreate
is optional and defaults to true. IfautoRefreshInterval
is not null, the repository will automatically
Properties
-
accessTokenBuilder
↔ (Future<
String> Function()?) -
A callback that returns the access token.
getter/setter pairinherited
- 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 the data from.
This is the only required parameter.
final
- 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 setterinherited
- 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.
finalinherited
- 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 -
This function is called on resolve to
get the data from the endpoint or cache.
override
-
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.
inherited
-
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.
inherited
-
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.
inherited
-
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.inherited -
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