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
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

delete(String endpoint, {dynamic body}) Future
DELETE: Sends a DELETE request to the specified endpoint with an optional body.
download(String url) 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 {}}) Future
GET: Sends a GET request to the specified endpoint with optional query parameters.
init({String baseUrl = '', String? contentType = "application/json", Duration? connectTimeout, Duration? receiveTimeout, Duration? sendTimeout, Map<String, dynamic>? headers, List<Interceptor> interceptors = const []}) → void
isConnected() Future<bool>
Checks if the device is connected to the internet by attempting to resolve 'google.com'.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
patch(String endpoint, dynamic body) Future
Sends a PATCH request to the specified endpoint with the provided body as JSON.
post(String endpoint, dynamic body) Future
POST: Sends a POST request to the specified endpoint with the provided body.
put(String endpoint, dynamic body) 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