ApiMate
Tired of writing repetitive try-catch, status code checks, and logging for every API call?
ApiMate is the simplest way to wrap Dio + Retrofit with clean success/failure handling, global config, and beautiful logging โ all in one line.
๐ฏ From this ๐
try {
final response = await dio.get('/user');
if (response.statusCode == 200) {
// success
} else {
// handle error
}
} catch (e) {
// error
}
โจ To this ๐
final result = await ApiMate(() => client.getUser()).call();
switch (result) {
case ApiMateSuccess(): print(result.data);
case ApiMateFailure(): print(result.errorMessage);
}
๐ Features
- โ
Supports both
Future<T>andFuture<HttpResponse<T>> - ๐ฆ Sealed result type:
ApiMateSuccess,ApiMateFailure - ๐ชต Pretty console logger for request/response/error
- ๐ Global config with
ApiMateConfig.enableLogging - ๐งผ Clean structure without callbacks or magic
๐ง Installation
dependencies:
api_mate: ^0.2.3
๐ ๏ธ Usage
1. Optional: Disable logging globally
void main() {
ApiMateConfig.enableLogging = false; // disable logs globally
runApp(MyApp());
}
2. Make an API call
final request = ApiMate(() => client.getPost(1));
final result = await request.call();
switch (result) {
case ApiMateSuccess<Post>():
print('โ
Success: ${result.data.title}');
break;
case ApiMateFailure():
print('โ Error: ${result.errorMessage}');
break;
}
3. Override logging per request (optional)
final request = ApiMate(
() => client.getPost(1),
enableLogging: true, // force enable for this call
);
await request.call();
๐ Folder Structure
lib/
api_mate/
api_mate.dart
api_mate_config.dart
api_mate_logger.dart
api_mate_result.dart
api_mate_exception.dart