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.connectionErrorinstead of waiting for timeout.getter/setter pair -
decoders
→ Map<
Type, dynamic> ? -
Decoders for morphing json into models
final
- 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
reasonthat 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
CancelTokenthat 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
DioExceptionresponse 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
requestcallback to call an APIhandleSuccessoverrides the response on a successful status codehandleFailureoverrides 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
onErrorcallback for the request -
onSuccess(
dynamic onSuccess(Response response, dynamic data)) → void -
Set the
onSuccesscallback for the request -
onUndefinedResponse(
dynamic data, Response response) → void -
Handle the undefined response's for HTTP requests.
The
dataparameter contains what was returned from your decoder. -
refreshToken(
Dio dio) → Future< void> -
Perform a
Diorequest to update the users auth token. This method is called when shouldRefreshToken returns true. You can override this method to perform your own request. Thedioparameter is a new instance ofDio. You can use this to perform a request without affecting the originalDioinstance. -
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
headersparameter contains the current headers. -
setBaseUrl(
String baseUrl) → void -
Set a
baseUrlfor the request. -
setBearerToken(
String bearerToken) → void -
Set a bearer token
headersto 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
connectTimeoutfor the request. -
setContentType(
String contentType) → void -
Set the
contentTypefor the request. -
setHeaders(
Map< String, dynamic> headers) → void -
Set new
headersto the baseOptions variable. -
setMethod(
String method) → void -
Set the
methodfor the request. -
setOptions(
BaseOptions baseOptions) → void -
Set the
baseOptionsfor 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
receiveTimeoutfor the request. -
setRetry(
int retry) → void -
Set how many times the request should
retryif 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
shouldRetryif theretryIfreturns true. -
setSendTimeout(
Duration duration) → void -
Set the
sendTimeoutfor the request. -
setShouldSetAuthHeaders(
bool shouldSetAuthHeaders) → void -
Set if the request should
shouldSetAuthHeadersif 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