BaseApi class abstract

Static HTTP client base class using Dio for making API requests.

This class provides a singleton Dio instance with common configuration for all API communications. It handles token injection, error processing, and timeout management automatically.

Usage

final response = await BaseApi.get('/api/users');
final response = await BaseApi.post('/api/create', data: payload);

Token Management

Call setToken after login to persist the Bearer token in SharedPrefsHelper. _mergeHeaders is the single injection point: it reads the token on every request so the correct value is always used. Call resetToken on logout. Register callbacks via addTokenListener to react to token changes.

Error Handling

Connection timeouts and service unavailability are caught and converted to user-friendly exception messages.

Example:

try {
  response = await BaseApi.get('/protected-endpoint');
} on Exception catch (e) {
  print('Service unavailable: $e');
}

Constructors

BaseApi()

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

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

addTokenListener(void listener(String?)) → void
Registers a listener to be called whenever the token changes.
configure({required String baseUrl, required String orgId, void onSessionExpired()?}) → void
Configures automatic JWT management via SdkTokenInterceptor.
delete(String path, {Object? data, Map<String, dynamic>? headers}) Future<Response>
Performs a DELETE request to the specified path.
get(String path, {Map<String, dynamic>? queryParameters, Map<String, dynamic>? headers}) Future<Response>
patch(String path, {Object? data, Map<String, dynamic>? headers}) Future<Response>
Performs a PATCH request to the specified path.
post(String path, {Object? data, Map<String, dynamic>? headers}) Future<Response>
Performs a POST request to the specified path.
put(String path, {Object? data, Map<String, dynamic>? headers}) Future<Response>
Performs a PUT request to the specified path.
removeTokenListener(void listener(String?)) → void
Removes a previously registered token listener.
resetToken() Future<void>
Resets (clears) the Bearer token from SharedPrefsHelper.
setDebugMode(bool enabled) → void
Sets whether to enable debug logging.
setDioForTesting(Dio dio) → void
Sets the internal Dio instance (useful for tests to inject a mocked Dio).
setToken(String? token) Future<void>
Sets the Bearer token for all subsequent requests.