NyApiService class

Inheritance

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.connectionError instead 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 reason that 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 CancelToken that 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 DioException response 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
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 request callback to call an API handleSuccess overrides the response on a successful status code handleFailure overrides the response on a failure
inherited
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 onError callback for the request
inherited
onSuccess(dynamic onSuccess(Response response, dynamic data)) → void
Set the onSuccess callback for the request
inherited
onUndefinedResponse(dynamic data, Response response) → void
Handle the undefined response's for HTTP requests. The data parameter 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 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 a new instance of Dio. You can use this to perform a request without affecting the original Dio instance.
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 headers parameter contains the current headers.
inherited
setBaseUrl(String baseUrl) → void
Set a baseUrl for the request.
inherited
setBearerToken(String bearerToken) → void
Set a bearer token headers to 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 connectTimeout for the request.
inherited
setContentType(String contentType) → void
Set the contentType for the request.
inherited
setHeaders(Map<String, dynamic> headers) → void
Set new headers to the baseOptions variable.
inherited
setMethod(String method) → void
Set the method for the request.
inherited
setOptions(BaseOptions baseOptions) → void
Set the baseOptions for 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 receiveTimeout for the request.
inherited
setRetry(int retry) → void
Set how many times the request should retry if 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 shouldRetry if the retryIf returns true.
inherited
setSendTimeout(Duration duration) → void
Set the sendTimeout for the request.
inherited
setShouldSetAuthHeaders(bool shouldSetAuthHeaders) → void
Set if the request should shouldSetAuthHeaders if 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