dio_result_handler 1.0.2 copy "dio_result_handler: ^1.0.2" to clipboard
dio_result_handler: ^1.0.2 copied to clipboard

A lightweight Flutter package for simplified API handling using Dio, with built-in success & failure result wrapping via ApiResult.

🚀 Flutter Dio API Handler #

A lightweight, clean, and scalable API handling layer built on top of Dio. Designed for simplicity with powerful error handling using ApiResult.


✨ Features #

  • ✅ Simple API calling structure
  • ✅ Built-in success & failure handling (ApiResult)
  • ✅ Centralized error handling
  • ✅ Clean and minimal architecture
  • ✅ Easy to plug into any Flutter project
  • ✅ Supports all HTTP methods (GET, POST, PUT, DELETE)

📦 Installation #

Add dependency:

dependencies:
  dio_api_handler: 1.0.0

⚙️ Setup #

Initialize the API client once (recommended in main.dart):

void main() {
  DioApiHandler.init(
    config: DioApiHandlerConfig(
      baseUrl: () => "api.example.com",
      token: () => null, // Optional
      onError: (dynamic error, int? statusCode) {
        debugPrint("Show Error pop up or message");
      },
    ),
  );

  runApp(const MyApp());
}

🔑 Set Token (Optional) #

ApiClient.setToken("your_access_token");

Remove token:

ApiClient.clearToken();

📡 Making API Calls #

Future<ApiResult<Map<String, dynamic>>> getAllUsers() {
  return ApiHandler.request<Map<String, dynamic>>(
    request: () => ApiClient.dio.get("/users"),
    response: (data) => data,
  );
}

📊 Handling Response #

final result = await getAllUsers();

if (result is ApiSuccess) {
  print(result.data);
} else if (result is ApiFailure) {
  print(result.message);
}

🧠 Custom Parsing #

Future<ApiResult<List<User>>> getUsers() {
  return ApiHandler.request<List<User>>(
    request: () => ApiClient.dio.get("/users"),
    response: (data) =>
        (data as List).map((e) => User.fromJson(e)).toList(),
  );
}

🏗️ Structure #

lib/
 ├── api_client.dart
 ├── api_handler.dart
 ├── api_result.dart
 └── exports.dart

❌ Error Handling #

All errors are automatically wrapped into:

ApiFailure(
  message: String,
  errorDetails: dynamic
  statusCode: int?,
)

🔄 Available Methods #

  • GET
  • POST
  • PUT
  • DELETE

(Handled via ApiClient.dio)


🔥 Best Practices #

  • Initialize ApiClient once
  • Use ApiHandler.request for all API calls
  • Avoid using Dio directly in UI
  • Parse response inside response callback

🚀 Future Improvements #

  • Token refresh & retry mechanism
  • Request logging toggle
  • Global error mapper
  • Pagination support

📄 License #

MIT License


💙 Support My Work #

If you find this package helpful, consider sponsoring 🙌

Sponsor

2
likes
140
points
32
downloads

Documentation

API reference

Publisher

verified publisherkhatrihub.site

Weekly Downloads

A lightweight Flutter package for simplified API handling using Dio, with built-in success & failure result wrapping via ApiResult.

License

MIT (license)

Dependencies

dio, flutter

More

Packages that depend on dio_result_handler