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
endpointwith an optionalbody. -
download(
String url) → Future -
DOWNLOAD:
Downloads a file from the specified
urland 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
endpointwith optionalqueryparameters. -
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
endpointwith the providedbodyas JSON. -
post(
String endpoint, dynamic body) → Future -
POST:
Sends a POST request to the specified
endpointwith the providedbody. -
put(
String endpoint, dynamic body) → Future -
PUT:
Sends a PUT request to the specified
endpointwith the providedbody. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited