ApiClient class abstract
A comprehensive API client for handling HTTP requests, file uploads, and downloads.
This abstract class provides a unified interface for all API operations in the application, including standard HTTP requests, file uploads (single/multiple), and file downloads. It automatically handles authentication, cookies, device information, and other common request requirements through configurable interceptors.
Key Features:
- Cross-platform file upload support (Web and Native)
- Automatic cookie management (Native platforms)
- Device information headers
- Language localization headers
- Timezone and timestamp headers
- Token refresh handling for authentication
- Persistent URL configuration
Implementation Requirements: Subclasses must implement the baseUrl getter to specify the API endpoint.
Usage Example:
class MyApiClient extends ApiClient {
@override
String get baseUrl => 'https://api.example.com';
}
final client = MyApiClient(
shouldUseDeviceInfo: true,
shouldUseLanguage: true,
);
// Upload a file
final file = await client.uploadFile(filePath: '/path/to/file.jpg');
// Download content
final bytes = await client.downloadBytes('https://example.com/image.png');
Constructors
Properties
- baseUrl → String
-
The base URL for API requests.
no setter
-
no setter
- dio → Dio
-
The Dio HTTP client instance used for all network requests.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- persistentStorage → PersistentStorage
-
no setter
- refreshInterceptor ↔ RefreshInterceptor
-
The refresh interceptor for handling token refresh on 401 errors.
getter/setter pair
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- secureStorage → SecureStorage
-
no setter
Methods
-
downloadBytes(
String url) → Future< Uint8List> - Downloads content as bytes from a URL.
-
downloadFile(
{required String url, required String savePath, required String filename}) → Future< File?> - Downloads content directly to a file on the local file system.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
uploadFile(
{String? filePath, Uint8List? bytes, String uploadUrl = "/upload-file", String? filename}) → Future< File> - Uploads a single file.
-
uploadFileFromFilePicker(
PlatformFile file, {String uploadUrl = "/upload-file"}) → Future< File> -
Uploads a file using
PlatformFile. -
uploadFileFromImagePicker(
XFile file, {String uploadUrl = "/upload-file"}) → Future< File> -
Uploads a file using
XFile. -
uploadFiles(
{List< String> ? filePaths, List<Uint8List> ? filesBytes, List<String> ? filenames, String uploadUrl = "/multi-upload-file"}) → Future<List< File> > - Uploads multiple files.
-
uploadFilesFromFilePicker(
List< PlatformFile> files, {String uploadUrl = "/multi-upload-file"}) → Future<List< File> > -
Uploads multiple files using
PlatformFile. -
uploadFilesFromImagePicker(
List< XFile> files, {String uploadUrl = "/multi-upload-file"}) → Future<List< File> > -
Uploads multiple files using
XFile.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited