ApiServices class abstract interface

A typed HTTP client wrapping Dio. All methods return Either<Failure, Response> — use .fold(onLeft, onRight) to handle errors and successes without exceptions.


Setup

Call configure once at app startup (before any request) to enable token auth and automatic token refresh on 401:

ApiServices.configure(
  getToken: () async => await storage.read('token'),
  onTokenRefresh: () async => await authRepo.refresh(),
  onRefreshFailed: () => Get.offAllNamed(Routes.login),
);

If you don't need token auth, skip configure and use instance directly.


Making requests

final result = await ApiServices.instance().get(endpoint: '/users');

result.fold(
  (failure) => print(failure.message),
  (response) => print(response.data),
);

Or inject via your DI framework and wrap in your own service layer:

final result = await _apiServices.post(
  endpoint: '/orders',
  data: order.toJson(),
);

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

cancelRequest({String cancellationReason = ''}) → void
Cancels all in-flight requests.
delete({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, bool showLoader = true}) Future<Either<Failure, Response>>
Sends a DELETE request to endpoint.
download({required String endpoint, required String savePath, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, bool deleteOnError = true, bool showLoader = true}) Future<Either<Failure, Response>>
Downloads the file at endpoint and streams it directly to savePath, instead of loading the whole response into memory like get would.
get({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, bool showLoader = true}) Future<Either<Failure, Response>>
Sends a GET request to endpoint.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, bool showLoader = true}) Future<Either<Failure, Response>>
Sends a PATCH request to endpoint.
post({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, bool showLoader = true}) Future<Either<Failure, Response>>
Sends a POST request to endpoint.
put({required String endpoint, Object? data, Map<String, dynamic>? params, Duration? receiveTimeout, Duration? sendTimeout, Map<String, String>? headers, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, CancelToken? cancelToken, bool showLoader = true}) Future<Either<Failure, Response>>
Sends a PUT request to endpoint.
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

configure({required Future<String?> getToken(), required Future<bool> onTokenRefresh(), void onRefreshFailed()?, Map<String, String> headerBuilder(String token)?, bool bypassConnectivityCheck = false, Duration refreshTimeout = const Duration(seconds: 30), ApiLogOptions logging = const ApiLogOptions()}) → void
Configures ApiServices with token-based authentication and automatic token refresh on 401 responses.
configureLoader({required void onShow(), required void onHide()}) → void
Configures a global loading indicator shown automatically around every request, so callers don't need to manage an isLoading flag per screen.
instance([Dio? dio]) ApiServices
Returns the singleton instance.
setConnectivityCheck({bool enabled = true}) → void
Controls the connectivity check without calling configure.
setLogging(ApiLogOptions options) → void
Configures the console logger without calling configure.