ApiClient class

ApiClient is a singleton class that provides a convenient interface for making HTTP requests using the Dio package. It supports GET, POST, PUT, DELETE, file downloads, and multipart uploads.

Features:

  • Configurable base URL, headers, timeouts, and interceptors.
  • Built-in logging interceptor for request/response logging.
  • Error handling for network and server errors.
  • Utility for checking internet connectivity.
  • Automatic MIME type detection for file uploads.

Example usage:

ApiClient.instance.init(baseUrl: "https://api.example.com");
final data = await ApiClient.instance.get("/endpoint");

Properties

hashCode int
The hash code for this object.
no setterinherited
isApiLoggerEnable bool
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

delete(String endpoint, {dynamic body, Map<String, dynamic> query = const {}, CancelToken? cancelToken, Options? options}) Future
DELETE: Sends a DELETE request to the specified endpoint with an optional body.
download(String url, {Map<String, dynamic> query = const {}, CancelToken? cancelToken, Object? data, Options? options, dynamic onSendProgress(int, int)?, dynamic onReceiveProgress(int, int)?, bool deleteOnError = true, FileAccessMode fileAccessMode = FileAccessMode.write, String lengthHeader = Headers.contentLengthHeader}) Future
DOWNLOAD: Downloads a file from the specified url and saves it to the user's downloads directory.
get(String endpoint, {Map<String, dynamic> query = const {}, Object? data, CancelToken? cancelToken, Options? options, dynamic onReceiveProgress(int, int)?}) Future
GET: Sends a GET request to the specified endpoint with optional query parameters.
getLogs() Future<List<LogModel>>
init({String baseUrl = '', String? contentType = "application/json", Duration? connectTimeout, Duration? receiveTimeout, Duration? sendTimeout, Map<String, dynamic>? headers, List<Interceptor> interceptors = const [], bool enableApiLogger = false}) → void
isConnected() Future<bool>
Checks if the device is connected to the internet by attempting to resolve 'google.com'.
multipart(String endpoint, FormData data, {Map<String, dynamic> query = const {}, CancelToken? cancelToken, Options? options, dynamic onSendProgress(int, int)?, dynamic onReceiveProgress(int, int)?}) Future
MULTIPART: Sends a multipart/form-data POST request to the specified endpoint with files.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch(String endpoint, dynamic body, {Map<String, dynamic> query = const {}, CancelToken? cancelToken, Options? options, dynamic onSendProgress(int, int)?, dynamic onReceiveProgress(int, int)?}) Future
Sends a PATCH request to the specified endpoint with the provided body as JSON.
post(String endpoint, dynamic body, {Map<String, dynamic> query = const {}, CancelToken? cancelToken, Options? options, dynamic onSendProgress(int, int)?, dynamic onReceiveProgress(int, int)?}) Future
POST: Sends a POST request to the specified endpoint with the provided body.
put(String endpoint, dynamic body, {Map<String, dynamic> query = const {}, CancelToken? cancelToken, Options? options, dynamic onSendProgress(int, int)?, dynamic onReceiveProgress(int, int)?}) Future
PUT: Sends a PUT request to the specified endpoint with the provided body.
toString() String
A string representation of this object.
inherited

Operators

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

Static Properties

instance ApiClient
final