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": "[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

1
likes
160
points
152
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