DioApiService class

Base API Service class

Implementers

Constructors

DioApiService({BaseOptions baseOptions(BaseOptions baseOptions)?, Dio initDio(Dio api)?})

Properties

activeRequestCount → int
Get the count of active (non-cancelled) requests.
no setter
baseOptions ↔ BaseOptions?
Base options for the request
getter/setter pair
baseUrl → String
Base URL for the request
final
checkConnectivityBeforeRequest ↔ bool
Whether to check connectivity before making a request. If true and the device is offline, the request will fail immediately with a DioExceptionType.connectionError instead of waiting for timeout.
getter/setter pair
decoders → Map<Type, dynamic>?
Decoders for morphing json into models
no setter
dio → Dio
Get the Dio instance for making custom requests
no setter
hashCode → int
The hash code for this object.
no setterinherited
interceptors → Map<Type, Interceptor>
Interceptors for the request
no setter
retry ↔ int
how many times should the request retry
getter/setter pair
retryDelay ↔ Duration
how long should the request wait before retrying
getter/setter pair
retryIf ↔ bool Function(DioException dioException)?
should the request retry if the retryIf callback returns true
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
shouldSetAuthHeaders ↔ bool
should the request retry if the retryIf callback returns true
getter/setter pair
useHttpOnResponse → bool
Use HTTP on response
final
useInterceptors → bool
Use interceptors
no setter

Methods

cancelAllRequests([String? reason]) → void
Cancel all active requests that were created with createCancelToken. Optionally provide a reason that will be included in the cancellation error.
clearAllCache() → Future<void>
Clear all API cache entries.
clearCache(String cacheKey) → Future<void>
Clear a specific cache entry.
createCancelToken() → CancelToken
Create a managed CancelToken that is tracked by this service. Use this to create tokens that can be cancelled with cancelAllRequests.
createEnhancedResponse<T>(Response response) → NyResponse<T>
Creates an enhanced API response from a Dio response and type This is a convenience method for creating NyResponse objects
error(DioException dioException) → void
Handle the DioException response if there is an issue.
extractData<T>(NyResponse<T> enhancedResponse) → T?
Convenience method to extract just the data from an enhanced response This helps with migration from the old handleResponse method
getDataIfSuccessful<T>(NyResponse<T> enhancedResponse) → T?
Convenience method to check if an enhanced response is successful and return the data or null if not successful
handleResponse<T>(Response response, {dynamic handleSuccess(NyResponse<T> response)?, dynamic handleFailure(NyResponse<T> response)?}) → NyResponse<T>
Handles an API network response from Dio. Returns a comprehensive NyResponse object containing the original Dio Response, morphed data, and useful utilities.
init() → void
Initialize class
network<T>({required dynamic request(Dio api), dynamic handleSuccess(NyResponse<T> response)?, dynamic handleFailure(NyResponse<T> response)?, String? bearerToken, String? baseUrl, bool useUndefinedResponse = true, bool shouldRetry = true, bool? shouldSetAuthHeaders, int? retry, Duration? retryDelay, bool retryIf(DioException dioException)?, Duration? connectionTimeout, Duration? receiveTimeout, Duration? sendTimeout, Duration? cacheDuration, String? cacheKey, CachePolicy? cachePolicy, bool? checkConnectivity, Map<String, dynamic>? headers}) → Future<T?>
Networking class to handle API requests Use the request callback to call an API handleSuccess overrides the response on a successful status code handleFailure overrides the response on a failure
networkResponse<T>({required dynamic request(Dio api), dynamic handleSuccess(NyResponse<T> response)?, dynamic handleFailure(NyResponse<T> response)?, String? bearerToken, String? baseUrl, bool useUndefinedResponse = true, bool shouldRetry = true, bool? shouldSetAuthHeaders, int? retry, Duration? retryDelay, bool retryIf(DioException dioException)?, Duration? connectionTimeout, Duration? receiveTimeout, Duration? sendTimeout, Duration? cacheDuration, String? cacheKey, CachePolicy? cachePolicy, bool? checkConnectivity, Map<String, dynamic>? headers}) → Future<NyResponse<T>>
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
onError(dynamic onError(DioException dioException)) → void
Set the onError callback for the request
onSuccess(dynamic onSuccess(Response response, dynamic data)) → void
Set the onSuccess callback for the request
onUndefinedResponse(dynamic data, Response response) → void
Handle the undefined response's for HTTP requests. The data parameter contains what was returned from your decoder.
refreshToken(Dio dio) → Future<void>
Perform a Dio request to update the users auth token. This method is called when shouldRefreshToken returns true. You can override this method to perform your own request. The dio parameter is this service's base Dio client, so the request shares the configured base URL, headers, and connection pool.
removeCancelToken(CancelToken token) → void
Remove a specific cancel token from the tracked list. Call this when a request completes to clean up the token.
setAuthHeaders(RequestHeaders headers) → Future<RequestHeaders>
Set the auth headers for the request. The headers parameter contains the current headers.
setBaseUrl(String baseUrl) → void
Set a baseUrl for the request.
setBearerToken(String bearerToken) → void
Set a bearer token headers to the baseOptions variable.
setCache(Duration? duration, String cacheKey) → void
Set the cache for the request.
setCheckConnectivityBeforeRequest(bool check) → void
Set whether to check connectivity before making requests. When enabled, requests will fail immediately if the device is offline.
setConnectTimeout(Duration duration) → void
Set the connectTimeout for the request.
setContentType(String contentType) → void
Set the contentType for the request.
setHeaders(Map<String, dynamic> headers) → void
Set new headers to the baseOptions variable.
setMethod(String method) → void
Set the method for the request.
setOptions(BaseOptions baseOptions) → void
Set the baseOptions for the request.
setPagination(int page, {String? paramPage, String? paramPerPage, String? perPage}) → void
Apply a pagination query to the HTTP request
setReceiveTimeout(Duration duration) → void
Set the receiveTimeout for the request.
setRetry(int retry) → void
Set how many times the request should retry if it fails.
setRetryDelay(Duration retryDelay) → void
Set the Duration how long the request should wait before retrying.
setRetryIf(bool retryIf(DioException dioException)) → void
Set if the request should shouldRetry if the retryIf returns true.
setSendTimeout(Duration duration) → void
Set the sendTimeout for the request.
setShouldSetAuthHeaders(bool shouldSetAuthHeaders) → void
Set if the request should shouldSetAuthHeaders if the shouldRefreshToken returns true.
shouldRefreshToken() → Future<bool>
Check if the users auth token should be refreshed. This method is called before every request. You can override this method to perform your own checks.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited