api_helper_rana 1.0.1
api_helper_rana: ^1.0.1 copied to clipboard
A robust Flutter package for API integration with Hive caching, background isolates, and auto-token refresh.
api_helper #
A powerful Flutter package for robust API integration with built-in caching, isolate support, and auto-token refresh.
Features #
- Standard HTTP Methods: Support for GET, POST, PUT, DELETE, and MULTIPART.
- Local Caching (Hive): Optional caching for any request. For GET requests, it supports a "Cache-then-Network" strategy.
- Isolate Support: Perform heavy parsing or requests in a background isolate to keep the UI smooth.
- Token Management: Automatic Bearer token insertion and refresh token handling via interceptors.
- Comprehensive Logging: Detailed request/response logs with support for manual user logs.
- State Management Agnostic: Works seamlessly with Provider, BLoC, GetX, Riverpod, etc.
Getting Started #
1. Initialization #
Initialize the ApiClient with your base URL and optional token handlers.
final apiClient = ApiClient(
baseUrl: "https://api.example.com",
getToken: () async => "your_token",
onRefreshToken: () async => { /* refresh logic */ },
);
await apiClient.init(); // Initialize Hive
2. Basic Requests #
Use the helper methods for quick requests.
final response = await apiClient.get<Map<String, dynamic>>("/users");
3. Cache-then-Network Strategy #
Enable saveInLocal and provide an onUpdate callback.
final response = await apiClient.get<List<dynamic>>(
"/posts",
options: ApiRequestOptions(saveInLocal: true),
onUpdate: (updatedResponse) {
// Update UI with fresh data
},
);
4. Background Isolates #
Use the isIsolate flag to run the request in a separate isolate.
final response = await apiClient.get(
"/large-data",
options: ApiRequestOptions(isIsolate: true),
);