FlutterApiCraft class

The main entry point for all API calls in the flutter_api_craft package.

Create an instance, configure it, then call call to execute the request. The constructor accepts every option you'd find in Postman, so the complete call configuration lives in one place.

// GET request
final res = await FlutterApiCraft(
  baseUrl: 'https://jsonplaceholder.typicode.com',
  path: '/posts',
  apiParams: ApiParams.simple({'_limit': '5'}),
).call();

// POST with Bearer auth + loading + snackbar + auto-pop
final res = await FlutterApiCraft(
  baseUrl: 'https://api.example.com',
  path: '/auth/login',
  apiType: ApiType.post,
  apiAuthorization: ApiAuthorization.bearer(token),
  apiBody: ApiBody.json({'email': email, 'password': pass}),
  enableLoading: true,
  enableApiSuccessResponseGetSnackBar: true,
  enableApiErrorResponseGetSnackBar: true,
  apiSuccessResponseNavigation: ApiSuccessNavigation.pop(),
).call();

// Multipart form-data (profile photo upload)
final res = await FlutterApiCraft(
  baseUrl: 'https://api.example.com',
  path: '/users/update-profile',
  apiType: ApiType.put,
  apiAuthorization: ApiAuthorization.bearer(token),
  apiBody: ApiBody.formData(
    fields: {'fullName': 'John Doe'},
    files: [ApiFile(fieldName: 'avatar', file: pickedFile)],
  ),
  enableLoading: true,
).call();

Constructors

FlutterApiCraft({required String baseUrl, required String path, ApiType apiType = ApiType.get, ApiBody? apiBody, Map<String, String> apiHeaders = const {}, ApiAuthorization? apiAuthorization, ApiParams? apiParams, ApiScript? apiScripts, ApiCookies? apiCookies, ApiSettings apiSettings = const ApiSettings(), bool enableLoading = false, String loadingMessage = 'Loading...', bool enableBodyResponseDebugPrint = false, bool enableApiSuccessResponseDebugPrint = false, bool enableApiSuccessResponseGetSnackBar = false, bool enableApiErrorResponseGetSnackBar = false, ApiSnackBarConfig? snackBarConfig, ApiSuccessNavigation? apiSuccessResponseNavigation, Future<String?> onUnauthorizedRefreshToken()?, int retryCount = 0, Duration retryDelay = const Duration(seconds: 1), void onSuccess(ApiResponse response)?, void onError(ApiResponse response)?, void onComplete()?, Client? httpClient})
Creates a FlutterApiCraft instance.
const

Properties

apiAuthorization ApiAuthorization?
Authorization configuration. Supports all Postman auth types.
final
apiBody ApiBody?
Request body. Supports none, JSON, form-data, URL-encoded, GraphQL, binary, XML, HTML, and JavaScript content types.
final
apiCookies ApiCookies?
Cookie jar configuration for automatic cookie management.
final
apiHeaders Map<String, String>
Extra request headers merged with the authorization headers.
final
apiParams ApiParams?
Query parameters appended to the URL.
final
apiScripts ApiScript?
Pre-request and post-response lifecycle hooks.
final
apiSettings ApiSettings
Low-level HTTP settings (redirects, timeouts, SSL).
final
apiSuccessResponseNavigation ApiSuccessNavigation?
Navigation action to perform after a successful response.
final
apiType ApiType
HTTP method. Defaults to ApiType.get.
final
baseUrl String
Base URL for all requests, e.g. "https://api.example.com".
final
enableApiErrorResponseGetSnackBar bool
Show a GetX snackbar on a failed response.
final
enableApiSuccessResponseDebugPrint bool
Print a formatted success log to the debug console.
final
enableApiSuccessResponseGetSnackBar bool
Show a GetX snackbar on a successful (2xx + success) response.
final
enableBodyResponseDebugPrint bool
Print the raw response body to the debug console.
final
enableLoading bool
Show a flutter_easyloading overlay while the request is in-flight.
final
hashCode int
The hash code for this object.
no setterinherited
httpClient → Client?
Optional HTTP client for unit testing.
final
loadingMessage String
Message displayed in the loading overlay. Defaults to "Loading...".
final
onComplete → void Function()?
Called after the request completes, regardless of outcome.
final
onError → void Function(ApiResponse response)?
Called with the final ApiResponse when the request fails.
final
onSuccess → void Function(ApiResponse response)?
Called with the final ApiResponse when the request succeeds.
final
onUnauthorizedRefreshToken Future<String?> Function()?
When the server returns 401, this callback is invoked to refresh the token. If it returns a non-null string the request is retried once with the new token as a Bearer token.
final
path String
Request path appended to baseUrl, e.g. "/auth/login".
final
retryCount int
Number of automatic retries on failure. Defaults to 0 (no retries).
final
retryDelay Duration
Delay between retry attempts. Defaults to 1 second.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
snackBarConfig ApiSnackBarConfig?
Full customization of the success and error snackbars.
final

Methods

call() Future<ApiResponse>
Executes the HTTP request and returns an ApiResponse.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited