api_caller 2.0.1 copy "api_caller: ^2.0.1" to clipboard
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": "bittu@example.com", }, 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

1
likes
160
points
120
downloads
screenshot

Publisher

verified publisherinfyrise.com

Weekly Downloads

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.

Homepage
Repository (GitHub)
View/report issues

Topics

#api #dio #http #network #dart

Documentation

API reference

License

MIT (license)

Dependencies

dio

More

Packages that depend on api_caller