api_caller 2.0.1
api_caller: ^2.0.1 copied to clipboard
A flexible API helper built on Dio with support for singleton usage, dynamic base URL override, per-request bearer token, JSON & FormData requests, and typed responses.
๐ฆ ApiHelper (Dio Based API Manager)
A singleton-based, reusable API helper built on top of Dio, supporting:
โ Global base URL & token
โ Per-API override (base URL + token)
โ GET / POST / PUT / DELETE / PATCH
โ JSON, www-form-urlencoded, multipart/form-data
โ Safe cloning (no data leak between APIs)
โ Clean & scalable architecture
๐ Features
One-time initialization
Dynamic bearer token update
Override base URL & token for only one API
Supports:
JSON body
www-form-urlencoded
multipart/form-data (file upload)
Centralized response handling
๐ Structure lib/ โโ api_caller.dart โโ models/ โ โโ api_caller_path_item.dart โ โโ api_caller_request_type.dart
๐ง Initialization (ONE TIME) ApiHelper.instance.init( baseUrl: "https://api.example.com", token: "GLOBAL_TOKEN_123", paths: [ ApiHelperPathItem.get("getUsers", "/users"), ApiHelperPathItem.post("addUser", "/users/add"), ApiHelperPathItem.post("uploadFile", "/upload"), ], );
โ This sets global base URL & token โ Can be used anywhere in app
๐ฅ GET Request (Normal) final res = await ApiHelper.instance.get("getUsers");
if (res.isSuccess) { print(res.value); } else { print(res.errorMessage); }
โก Uses global base URL + global token
๐ค POST JSON Data final res = await ApiHelper.instance.post( "addUser", data: { "name": "Bittu", "email": "[email protected]", }, contentType: Headers.jsonContentType, );
๐ค POST www-form-urlencoded final res = await ApiHelper.instance.post( "addUser", data: { "username": "demo_user", "password": "123456", }, contentType: Headers.formUrlEncodedContentType, );
๐ค POST multipart / Form-Data (File Upload) final formData = FormData.fromMap({ "title": "Profile Pic", "file": MultipartFile.fromBytes( [1, 2, 3, 4], filename: "image.png", ), });
final res = await ApiHelper.instance.post( "uploadFile", data: formData, contentType: Headers.multipartFormDataContentType, );
๐ Change Token Dynamically (Global) ApiHelper.instance.setToken("NEW_GLOBAL_TOKEN");
โก All APIs will now use the new token
๐ Override Base URL & Token (ONLY ONE API) final item = ApiHelper.instance.getPathItem("getUsers") ..setBaseUrlOverride("https://uat.example.com") ..setTokenOverride("UAT_ONLY_TOKEN");
final res = await ApiHelper.instance.request(item);
โ Override applies only to this request โ Other APIs remain unchanged
๐ Back to Normal Automatically final res = await ApiHelper.instance.get("getUsers");
โก Uses original global base URL & token again
๐ง Token Priority Order Request Token (highest) โ Path Override Token โ Global Token (lowest)
๐ง Base URL Priority Path Override Base URL โ Global Base URL
โ Error Handling if (!res.isSuccess) { print(res.errorMessage); }
Handled cases:
Network error
Timeout
4xx / 5xx status codes
Dio exceptions
โ Best Practices
โ Call init() only once โ Always use getPathItem() for overrides โ Never modify stored path directly โ Prefer override instead of new instance
๐ Conclusion
This ApiHelper provides a clean, scalable, and production-ready way to manage APIs in Flutter with:
Minimal boilerplate
Maximum flexibility
Safe override mechanism
If you want, I can also provide:
๐ฆ Flutter UI integration example
๐ Token refresh interceptor
๐งช Unit tests
๐งฉ Repository-pattern wrapper
