NyApiService class
- Inheritance
-
- Object
- DioApiService
- NyApiService
Constructors
-
NyApiService({Map<
Type, dynamic> ? decoders = const {}, BaseOptions baseOptions(BaseOptions baseOptions)?, Dio initDio(Dio api)?, NetworkLogger? networkLogger, bool? useNetworkLogger})
Properties
- activeRequestCount → int
-
Get the count of active (non-cancelled) requests.
no setterinherited
- baseOptions ↔ BaseOptions?
-
Base options for the request
getter/setter pairinherited
- baseUrl → String
-
Base URL for the request
finalinherited
- 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 pairinherited -
decoders
→ Map<
Type, dynamic> ? -
Map decoders to modelDecoders
final
- dio → Dio
-
Get the Dio instance for making custom requests
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
-
interceptors
→ Map<
Type, Interceptor> -
Default interceptors
no setteroverride
- networkLogger ↔ NetworkLogger?
-
Network logger instance
getter/setter pair
- retry ↔ int
-
how many times should the request retry
getter/setter pairinherited
- retryDelay ↔ Duration
-
how long should the request wait before retrying
getter/setter pairinherited
- retryIf ↔ bool Function(DioException dioException)?
-
should the request retry if the retryIf callback returns true
getter/setter pairinherited
- 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 pairinherited
- useHttpOnResponse → bool
-
Use HTTP on response
finalinherited
- useInterceptors → bool
-
Use interceptors
no setterinherited
- useNetworkLogger ↔ bool?
-
Whether to use the network logger interceptor
getter/setter pair
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.inherited -
clearAllCache(
) → Future< void> -
Clear all API cache entries.
inherited
-
clearCache(
String cacheKey) → Future< void> -
Clear a specific cache entry.
inherited
-
createCancelToken(
) → CancelToken -
Create a managed
CancelTokenthat is tracked by this service. Use this to create tokens that can be cancelled with cancelAllRequests.inherited -
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
inherited
-
delete<
T> (String url, {Object? data, Options? options, CancelToken? cancelToken}) → Future< T?> - Make a DELETE request
-
download(
String url, {required String savePath, ProgressCallback? onProgress, bool deleteOnError = true, CancelToken? cancelToken, Map< String, dynamic> ? queryParameters, Options? options}) → Future<Response> - Download a file from the given URL to a local path.
-
error(
DioException dioException) → void -
Handle the
DioExceptionresponse if there is an issue.inherited -
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
inherited
-
get<
T> (String url, {Object? data, Map< String, String> ? queryParameters, Options? options, CancelToken? cancelToken, ProgressCallback? onReceiveProgress}) → Future<T?> - Make a GET request
-
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
inherited
-
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.inherited -
head(
String url, {Object? data, Map< String, String> ? queryParameters, Options? options, CancelToken? cancelToken}) → Future<Response> - Make a HEAD request to check resource existence or get headers
-
init(
) → void -
Initialize class
inherited
-
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 failureinherited -
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> > -
inherited
-
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 requestinherited -
onSuccess(
dynamic onSuccess(Response response, dynamic data)) → void -
Set the
onSuccesscallback for the requestinherited -
onUndefinedResponse(
dynamic data, Response response) → void -
Handle the undefined response's for HTTP requests.
The
dataparameter contains what was returned from your decoder.inherited -
patch<
T> (String url, {Object? data, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) → Future< T?> - Make a PATCH request
-
post<
T> (String url, {Object? data, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) → Future< T?> - Make a POST request
-
put<
T> (String url, {Object? data, Options? options, CancelToken? cancelToken, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress}) → Future< T?> - Make a PUT request
-
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 this service's baseDioclient, so the request shares the configured base URL, headers, and connection pool.inherited -
removeCancelToken(
CancelToken token) → void -
Remove a specific cancel token from the tracked list.
Call this when a request completes to clean up the token.
inherited
-
setAuthHeaders(
RequestHeaders headers) → Future< RequestHeaders> -
Set the auth headers for the request.
The
headersparameter contains the current headers.inherited -
setBaseUrl(
String baseUrl) → void -
Set a
baseUrlfor the request.inherited -
setBearerToken(
String bearerToken) → void -
Set a bearer token
headersto the baseOptions variable.inherited -
setCache(
Duration? duration, String cacheKey) → void -
Set the cache for the request.
inherited
-
setCheckConnectivityBeforeRequest(
bool check) → void -
Set whether to check connectivity before making requests.
When enabled, requests will fail immediately if the device is offline.
inherited
-
setConnectTimeout(
Duration duration) → void -
Set the
connectTimeoutfor the request.inherited -
setContentType(
String contentType) → void -
Set the
contentTypefor the request.inherited -
setHeaders(
Map< String, dynamic> headers) → void -
Set new
headersto the baseOptions variable.inherited -
setMethod(
String method) → void -
Set the
methodfor the request.inherited -
setOptions(
BaseOptions baseOptions) → void -
Set the
baseOptionsfor the request.inherited -
setPagination(
int page, {String? paramPage, String? paramPerPage, String? perPage}) → void -
Apply a pagination query to the HTTP request
inherited
-
setReceiveTimeout(
Duration duration) → void -
Set the
receiveTimeoutfor the request.inherited -
setRetry(
int retry) → void -
Set how many times the request should
retryif it fails.inherited -
setRetryDelay(
Duration retryDelay) → void -
Set the Duration how long the request should wait before retrying.
inherited
-
setRetryIf(
bool retryIf(DioException dioException)) → void -
Set if the request should
shouldRetryif theretryIfreturns true.inherited -
setSendTimeout(
Duration duration) → void -
Set the
sendTimeoutfor the request.inherited -
setShouldSetAuthHeaders(
bool shouldSetAuthHeaders) → void -
Set if the request should
shouldSetAuthHeadersif the shouldRefreshToken returns true.inherited -
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.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
upload<
T> (String url, {required String filePath, String fieldName = 'file', Map< String, dynamic> ? additionalFields, ProgressCallback? onProgress, String? fileName, String? contentType, CancelToken? cancelToken}) → Future<T?> - Upload a file using multipart/form-data.
-
uploadMultiple<
T> (String url, {required Map< String, String> files, Map<String, dynamic> ? additionalFields, ProgressCallback? onProgress, CancelToken? cancelToken}) → Future<T?> - Upload multiple files using multipart/form-data.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited