Skip to main content

baaba_api_handler 4.0.0 copy "baaba_api_handler: ^4.0.0" to clipboard
baaba_api_handler: ^4.0.0 copied to clipboard

A typed, functional HTTP client for Flutter. Wraps Dio with Either-based error handling, token refresh, retries, request de-duplication, and a built-in test double.

baaba_api_handler #

A typed, functional HTTP client for Flutter. Wraps Dio with Either-based error handling, automatic token refresh, configurable retries, request de-duplication, and a built-in test double.

No exceptions cross the API boundary — every method returns Either<Failure, T>.

final result = await api.getAs<User>(endpoint: '/users/1', parser: User.fromJson);

result.fold(
  (failure) => emit(ErrorState(failure.message)),
  (user)    => emit(LoadedState(user)),
);

Upgrading from 2.0.0? 3.0.0 and 3.1.0 were never published, so the next version on pub.dev after 2.0.0 is 4.0.0. Follow both the 3.0.0 and 4.0.0 sections of MIGRATION.md. The most common steps: raise equatable to 3, replace the removed configure(), setConnectivityCheck() and setLogging() with ApiServices.init(ApiConfig(...)), move to Flutter 3.32 (Dart 3.8), drop any response-caching configuration, and add a case for ErrorSource.gatewayTimeout if you switch exhaustively. Coming from 1.x, follow the 2.0.0 section of the same guide first.

Using 3.x from git? 4.0.0 requires Flutter 3.32 (Dart 3.8), removes response caching and the api_cache_manager dependency, and adds ErrorSource.gatewayTimeout — see the 4.0.0 section of MIGRATION.md.

Table of Contents #


Installation #

dependencies:
  baaba_api_handler: ^4.0.0

Requires Flutter 3.32 / Dart 3.8 or newer. Works on Android, iOS, web, and desktop.

import 'package:baaba_api_handler/baaba_api_handler.dart';

Response, CancelToken, FormData (Dio) and Either (fpdart) are re-exported from this single import — you don't need dio or fpdart as direct dependencies.


Setup #

Call init once at app startup, before any request:

void main() {
  ApiServices.init(ApiConfig(
    baseUrl: 'https://api.example.com',
    auth: AuthConfig(
      getToken: () => storage.read(key: 'access_token'),
      onTokenRefresh: () => authRepository.refresh(),
      onRefreshFailed: () => authController.logout(),
    ),
  ));
  runApp(const MyApp());
}

Every field is optional. ApiServices.init(const ApiConfig()) gives a plain client with sane timeouts and no auth, and skipping init entirely also works — instance() builds a default client on first use.

Environments usually differ in a field or two, so build one base config and copyWith the rest:

const base = ApiConfig(connectTimeout: Duration(seconds: 20));

// staging: internal network, connectivity probe blocked by the proxy
ApiServices.init(base.copyWith(
  baseUrl: 'https://staging.example.com',
  bypassConnectivityCheck: true,
));

ApiServices.reset() clears the singleton, config, and loader callbacks, and cancels any requests still in flight on the old client — useful on logout, and in test tearDown.


Making requests #

final api = ApiServices.instance();

final result = await api.get(endpoint: '/users', params: {'page': 1});

result.fold(
  (failure) => print(failure.message),
  (response) => print(response.data),
);

get, post, put, patch, delete, upload, and download are available. With baseUrl set, endpoints are relative; an absolute endpoint is used as-is, so hitting a CDN or third-party host from the same client works fine.

Typed responses #

Each verb has an *As<T> variant that takes a parser and hands back the deserialized object, removing the fold → fromJson → try/catch block from every repository method:

final user = await api.getAs<User>(endpoint: '/users/1', parser: User.fromJson);
// Either<Failure, User>

For lists, listParser wraps a single-object parser:

// [{"id": 1}, {"id": 2}]
await api.getAs<List<User>>(endpoint: '/users', parser: listParser(User.fromJson));

// {"data": [{"id": 1}], "meta": {...}}
await api.getAs<List<User>>(
  endpoint: '/users',
  parser: listParser(User.fromJson, key: 'data'),
);

If the parser throws, you get a Failure with ErrorSource.parseError and the raw body in failure.data — the exception never reaches you.

Request parameters #

Shared by every HTTP method:

Parameter Type Description
endpoint String Path, relative to baseUrl, or an absolute URL.
data Object? Request body.
params Map<String, dynamic>? Query parameters.
headers Map<String, String>? Replaces the default application/json headers for this request.
receiveTimeout Duration? Overrides the configured receive timeout.
sendTimeout Duration? Overrides the configured send timeout.
cancelToken CancelToken? Cancels this specific request.
onSendProgress ProgressCallback? Upload progress.
onReceiveProgress ProgressCallback? Download progress.
showLoader bool Opt this request out of the global loader. Defaults to true.

get, getAs, head and options additionally accept dedupe — see De-duplication.

Response types #

Bodies are decoded as JSON by default. Pass responseType for anything else — an image you want in memory, or an endpoint that returns plain text:

// Raw bytes, without writing to disk the way download() does.
final logo = await api.get(
  endpoint: '/assets/logo.png',
  responseType: ResponseType.bytes,
);

// Text that isn't JSON.
final csv = await api.get(
  endpoint: '/reports/export.csv',
  responseType: ResponseType.plain,
);

It works on every method, including the *As<T> variants, where the parser then receives whatever the response type produced rather than decoded JSON.

For a file you want on disk rather than in memory, use download — it streams and never holds the whole body.


HEAD and OPTIONS #

head asks for headers without a body — useful to check that a resource exists, or to read its Content-Length before committing to a download:

final result = await api.head(endpoint: '/files/report.pdf');

final size = result.fold(
  (_) => null,
  (response) => response.headers.value('content-length'),
);

options asks the server which methods apply to a resource. Both are idempotent, so transient failures are retried just like a get.


Uploads #

final result = await api.upload(
  endpoint: '/profile/avatar',
  fields: {'userId': '42'},
  files: [UploadFile.fromPath(field: 'avatar', path: picked.path)],
  onSendProgress: (sent, total) => progress.value = sent / total,
);

Use UploadFile.fromBytes on web, where a picked file has no filesystem path:

UploadFile.fromBytes(field: 'avatar', bytes: pickedBytes, filename: 'avatar.png')

contentType is inferred from the filename when omitted. A JSON content-type is never sent with a multipart body, even if you pass one in headers — it would replace the multipart/form-data type Dio generates, boundary included.

Uploads default to POST; pass method: for an API that expects PUT or PATCH.

A timeout on an upload is never retried — the server may already have stored the file. A 429 or 503 is, because the server said it did not process the request, and so is a replay after a 401 token refresh, which matters for an upload long enough to outlive its token. In both cases the multipart body is rebuilt before resending: it is a stream, read once, and Dio throws if you hand it the consumed one.

Downloads #

Streams the response straight to disk rather than loading it into memory:

final dir = await getApplicationDocumentsDirectory();

final result = await api.download(
  endpoint: '/files/report.pdf',
  savePath: '${dir.path}/report.pdf',
  onReceiveProgress: (received, total) => print('${received / total * 100}%'),
);

Downloads, and requests with responseType: ResponseType.bytes or stream, send Accept: */* unless you pass your own headers — a JSON Accept gets a 406 from servers that honour it, static file handlers in particular.

Cancellation #

cancelRequest() cancels every in-flight request:

@override
void onClose() {
  api.cancelRequest(cancellationReason: 'Screen closed');
  super.onClose();
}

That is all-or-nothing, so a screen tearing down would abort requests belonging to screens still on the stack. Tag the requests you want to cancel together:

await api.get(endpoint: '/feed', tag: 'feed');
await api.get(endpoint: '/stories', tag: 'feed');

// Cancels only those two.
api.cancelRequest(tag: 'feed', cancellationReason: 'Left the feed');

Untagged requests are cancelled only by a call that omits tag. A tagged request opts out of de-duplication, for the same reason a caller-supplied CancelToken does: cancelling one tag must not abort a collapsed request another caller is still waiting on.

For a single request, pass your own CancelToken.


Handling failures #

Failure carries a user-facing message, a mapped code, and the raw response:

Field Description
message Human-readable. Extracted from the body's message, detail, error or msg key, then the field messages under errors, then title (ASP.NET's problem details), falling back to a generic string for the status.
errorType ErrorSource — semantic enum (notFound, unauthorized, parseError, …).
code ResponseCode — enum with an integer value. Collapses unrecognised statuses to defaultError.
statusCode The literal HTTP status, e.g. 418. null for failures that never reached the server.
data The raw response body. null for offline, timeout, and cancellation.
validationErrors Per-field errors parsed from an errors object. null when absent.
errorCode The server's own code — code or errorCode, top level or inside error. null when absent.
requestOptions The request that failed — method, path, headers. null when none was built. Excluded from equality.

Validation errors #

For a 422 body like {"message": "Validation failed", "errors": {"email": ["already taken"]}}:

result.fold(
  (failure) {
    final fields = failure.validationErrors;
    if (fields != null) {
      emailError.value = fields['email']?.first;
      nameError.value = fields['name']?.first;
    } else {
      showSnackBar(failure.message);
    }
  },
  (user) => ...,
);

Success that isn't #

Some APIs answer 200 OK with {"success": false, "message": "..."}. Without help, those land in Right and every caller has to re-check the body. isSuccess routes them to Failure instead, pulling the message out the same way a real error response would:

ApiServices.init(ApiConfig(
  isSuccess: (response) {
    final data = response.data;
    return data is! Map || data['success'] != false;
  },
));

By itself that produces a generic badRequest failure, which throws away any error code the body carried. Add onRejected to build the Failure yourself:

ApiServices.init(ApiConfig(
  isSuccess: (r) => r.data is! Map || r.data['success'] != false,
  onRejected: (r) => Failure(
    ErrorSource.forbidden,
    ResponseCode.forbidden,
    r.data['message'] as String? ?? 'Request failed',
    data: r.data,
    statusCode: r.statusCode,
  ),
));

Return null to fall back to the generic failure. It is only consulted when isSuccess returns false — or throws, which counts as a rejection rather than escaping as an exception.

isSuccess is not applied to head() or download() — neither has a body in response.data to judge.

Mapping failures for your app #

mapFailure sees every Failure before the caller does — HTTP errors, timeouts, offline, cancellation, parse errors, and rejected bodies — so app-specific wording and codes live in one place instead of every repository:

ApiServices.init(ApiConfig(
  mapFailure: (failure) {
    // A 401 from login is a wrong password, not an expired session.
    if (failure.statusCode == 401 &&
        failure.requestOptions?.path == '/auth/login') {
      return Failure(
        ErrorSource.unauthorized,
        ResponseCode.unauthorized,
        'Wrong employee ID or password.',
        data: failure.data,
        statusCode: 401,
      );
    }
    if (failure.errorCode == 'ACCOUNT_LOCKED') {
      return Failure(
        failure.errorType,
        failure.code,
        'Your account is locked. Contact HR.',
        data: failure.data,
        statusCode: failure.statusCode,
      );
    }
    return null; // keep it as it is
  },
));

A returned failure without requestOptions gets the original's. The observer reports the mapped failure, and FakeApiServices applies the mapper too. A mapper that throws leaves the failure unchanged.


Authentication #

AuthConfig attaches a token to every request and refreshes it on 401:

ApiServices.init(ApiConfig(
  auth: AuthConfig(
    getToken: () => storage.read(key: 'access_token'),
    onTokenRefresh: () => authRepository.refresh(),   // true on success
    onRefreshFailed: () => authController.logout(),
    refreshTimeout: const Duration(seconds: 20),
  ),
));

A burst of concurrent 401s — the usual shape of a token expiring — all wait on one refresh and retry, rather than only the first succeeding. refreshTimeout bounds that wait. A refresh request made through the same client that itself 401s — an expired refresh token — fails at once, so onRefreshFailed fires without delay. A 401 that arrives after another request's refresh has already finished is replayed with the new token rather than triggering a second refresh.

By default the token goes out as Authorization: Bearer <token>. headerBuilder changes that:

AuthConfig(
  getToken: () => storage.read(key: 'token'),
  onTokenRefresh: () => authRepo.refresh(),
  headerBuilder: (token) => {
    'Authorization': 'Token $token',
    'X-Tenant-Id': 'my-org',
  },
)

Omit auth entirely for an unauthenticated client; 401s then surface as an ordinary Failure.

Login, logout and other requests that must not refresh #

A 401 from the login endpoint means a wrong password. Without help, it starts a refresh with nothing to refresh, which fails and fires onRefreshFailed — logging the user "out" of the login screen. Name those requests in skipRefresh:

auth: AuthConfig(
  getToken: getToken,
  onTokenRefresh: refresh,
  skipRefresh: (options) => const {
    '/auth/login',
    '/auth/refresh',
    '/auth/logout',
  }.contains(options.path),
),

Their 401 surfaces as an ordinary Failure. They still carry the token when there is one, since logout usually needs it.

Keeping the session when a refresh can't be tried #

onTokenRefresh returns a bool, so a refresh that failed because the device was offline ends the session just as a refused refresh token does. Use refreshSession instead to tell the two apart:

auth: AuthConfig(
  getToken: getToken,
  refreshSession: () async {
    try {
      await authRepository.refresh();
      return RefreshOutcome.refreshed;
    } on DioException catch (e) {
      final status = e.response?.statusCode;
      return status == 400 || status == 401 || status == 403
          ? RefreshOutcome.sessionExpired   // fires onRefreshFailed
          : RefreshOutcome.failed;          // keeps the session
    }
  },
  onRefreshFailed: () => authController.logout(),
),
Outcome Waiting requests onRefreshFailed
refreshed Replayed with the new token. —
sessionExpired Fail with their 401. Fires.
failed Fail with their 401; a later request tries again. Does not fire.

Supply exactly one of onTokenRefresh and refreshSession. A throw from refreshSession counts as failed; a throw from onTokenRefresh still ends the session, as before.

Refreshing before the token expires #

With isTokenExpired, a request whose token has run out refreshes before it is sent, instead of spending a round trip on a 401:

auth: AuthConfig(
  getToken: getToken,
  refreshSession: refresh,
  isTokenExpired: () async {
    final expiresAt = await storage.readExpiry();
    return expiresAt != null &&
        DateTime.now().add(const Duration(seconds: 30)).isAfter(expiresAt);
  },
),

Concurrent requests share the one refresh. If it ends the session, the request is not sent and fails as a 401 (ErrorSource.unauthorized); if it merely fails, the request goes out with the token it has, and a 401 for it does not try the refresh again. Not asked for skipRefresh requests, or for requests made by the refresh itself. A throwing check counts as "not expired".

Which hosts get the token #

By default, only the host in your baseUrl.

That matters because baseUrl supports absolute endpoints, so one client can reach a CDN, an S3 presigned URL, or a third-party API. Sending the token to all of them leaks your session to whoever the caller names — and it breaks presigned URLs outright, since S3 rejects a request carrying both a presigned signature and an Authorization header.

To span several hosts you own:

auth: AuthConfig(
  getToken: getToken,
  onTokenRefresh: refresh,
  sendTokenTo: (uri) => uri.host.endsWith('.mycompany.com'),
),

The check is by host, so a different port or scheme on the same host still receives the token — use the predicate if that matters to you. A predicate that throws is treated as "don't send". When baseUrl isn't set there's nothing to compare against and the token goes everywhere.

A 401 from a host the token was never sent to won't trigger a refresh, which also means a third party's 401 can't cascade into onRefreshFailed and sign your user out.


Interceptors #

ApiObserver can watch a request but not change it. For anything that needs to modify one — a correlation id, a tenant header computed at call time, request signing, a router that serves fixtures during local development — pass your own Dio interceptors:

ApiServices.init(ApiConfig(
  interceptors: [
    InterceptorsWrapper(onRequest: (options, handler) {
      options.headers['X-Correlation-Id'] = const Uuid().v4();
      handler.next(options);
    }),
  ],
));

They sit after auth and before retry. After auth, so a signing interceptor sees the Authorization header it has to cover. Before retry and the logger, so a replayed request passes through them again — a signature carrying a timestamp is regenerated per attempt rather than replayed stale — and so what you add is what gets logged.

They run in the order given, and are applied when the client is built, so changing them means calling init again.

A retry replays the same RequestOptions instance rather than a copy. Read a header off it after the fact and you see the last attempt's value, not the one that attempt sent.


Concurrency #

A screen that fires twenty requests on mount sends all twenty at once. On a mobile connection that saturates the connection pool — every request reports a worse latency than it would have alone — and it is a reliable way to trip server-side rate limiting that your retry policy then has to clean up.

ApiServices.init(const ApiConfig(maxConcurrentRequests: 6));

Requests past the cap queue in the order they were made and start as slots free up. The cap governs actual network calls: a request collapsed by de-duplication doesn't consume a slot of its own. A queued request is still cancellable, and still holds the loading indicator — it's pending, not finished.

Unset by default, which is unlimited.


Connectivity checks #

Every request runs a pre-flight connectivity probe, so an offline call fails fast with ErrorSource.noInternetConnection instead of waiting out a timeout. A positive result is reused for connectivityCacheTtl (5s by default); a negative one is never cached, since that is exactly when the user is hammering retry.

The default probe reaches third-party hosts. That is wrong often enough to matter — corporate networks block it, privacy reviews object to it, and it says nothing about whether your API is up. Point it at your own health endpoint:

ApiServices.init(ApiConfig(
  connectivityProbe: () async {
    try {
      final res = await Dio().head('https://api.example.com/health');
      return res.statusCode == 200;
    } catch (_) {
      return false;
    }
  },
));

It must not throw — one that does is read as "offline". To skip the check entirely, use bypassConnectivityCheck: true.


De-duplication #

Two identical GETs (or HEAD/OPTIONS requests) in flight at the same time share one network call — the common case being several widgets on one screen asking for the same resource. Pass dedupe: false to opt out. "Identical" means the same url, query, body, headers and responseType — a request for bytes is never handed another caller's decoded JSON. It is skipped automatically when you supply your own CancelToken or a tag, since cancelling one caller must not cancel the other, and when you pass a progress callback, which only one caller's request could drive.

De-duplication only collapses overlapping calls: sequential requests each hit the network. The package does not cache responses — if you need offline data, store it in your own persistence layer.


Retries #

Transient failures are retried automatically. RetryPolicy controls the details:

ApiServices.init(const ApiConfig(
  retry: RetryPolicy(maxRetries: 5, baseDelay: Duration(seconds: 1)),
));

// or turn it off entirely
ApiServices.init(const ApiConfig(retry: RetryPolicy.disabled()));
Field Default Description
maxRetries 3 Attempts after the first. 0 disables retries.
baseDelay 500ms Attempt n waits up to baseDelay * 2^(n-1).
maxDelay 30s Ceiling on any single wait.
useJitter true Spread retries randomly across the backoff window.
respectRetryAfter true Honour the server's Retry-After header.
retryableStatusCodes {408, 429, 500, 502, 503, 504} Which statuses are worth retrying.
retryIf null Final veto: (error, attempt) => bool.

What gets retried, and why it depends on the method. Transport errors (timeouts, connection failures) are retried for idempotent methods only — if the server already processed a POST before the timeout, repeating it could create the same order twice. Status codes split the same way: 429 and 503 mean the server explicitly did not process the request, so those are safe for any method; 408, 500, 502, and 504 are ambiguous and stay gated on idempotency.

Retry-After is understood in both permitted forms — delta-seconds and HTTP-date — and clamped to maxDelay. Backoff uses full jitter, so a burst of requests that fail together don't all retry in lockstep and hammer a recovering server.


Observability #

One hook for every request, response, and failure:

class SentryObserver extends ApiObserver {
  @override
  void onFailure(Failure failure, RequestOptions? options) {
    Sentry.captureMessage(
      '${options?.method} ${options?.path} → ${failure.code.value}',
      level: SentryLevel.warning,
    );
  }
}

ApiServices.init(ApiConfig(observer: SentryObserver()));

onResponse and onFailure fire exactly once per call you made, including for failures Dio never produces an exception for — offline short-circuits, parse errors, and bodies rejected by isSuccess. onRequest fires per network attempt, so a retried request reports more than once.

An observer that throws can never break a request.


Loading indicator #

Register a global indicator and every request shows it automatically:

// GetX
ApiServices.configureLoader(
  onShow: () => Get.dialog(const LoadingDialog(), barrierDismissible: false),
  onHide: () => Get.back(),
);

// Navigator with a global key
ApiServices.configureLoader(
  onShow: () => showDialog(
    context: navigatorKey.currentContext!,
    barrierDismissible: false,
    builder: (_) => const LoadingDialog(),
  ),
  onHide: () => navigatorKey.currentState!.pop(),
);

The package has no UI dependency — these are plain closures. Calls are reference-counted, so concurrent requests share one indicator: onShow fires for the first, onHide only once every in-flight request has finished, whether it succeeded, failed, or threw. Pass showLoader: false to opt a request out, e.g. background polling.


Certificate pinning and proxies #

Supply your own HttpClientAdapter. The package deliberately ships no implementation: the dart:io-based adapters don't exist on web, and bundling one would cost the package its platform neutrality.

import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:dio/io.dart';

const _pinnedSha256 = 'AA:BB:CC:...';

ApiServices.init(ApiConfig(
  httpClientAdapter: IOHttpClientAdapter(
    createHttpClient: () => HttpClient()
      ..badCertificateCallback = (cert, host, port) {
        final fingerprint = sha256.convert(cert.der).toString();
        return fingerprint == _pinnedSha256;
      },
  ),
));

The same hook routes traffic through Charles or Proxyman while debugging:

httpClientAdapter: IOHttpClientAdapter(
  createHttpClient: () => HttpClient()
    ..findProxy = (uri) => 'PROXY 192.168.1.10:8888'
    ..badCertificateCallback = (_, __, ___) => true,   // debug builds only
),

Logging #

PrettyDioLogger is attached outside release builds only. ApiLogOptions controls what it prints:

ApiServices.init(ApiConfig(
  logging: ApiLogOptions(
    requestHeader: true,
    responseHeader: true,
    maxWidth: 120,
    logPrint: (line) => myLogger.debug(line.toString()),
  ),
));
Field Default Description
enabled true Master switch. ApiLogOptions.disabled() turns everything off.
request true Log the request line.
requestHeader false Log request headers.
requestBody true Log the request body.
responseHeader false Log response headers.
responseBody true Log the response body.
error true Log errors.
maxWidth 90 Wrap width.
compact true Compact JSON output.
trimBase64 false Collapse base64 blobs — see below.
logPrint debugPrint Where lines go.

Release builds never attach a logger, whatever this says.

Base64 in log output #

An API that returns photographs or fingerprints inline will drown your console. The logger wraps every value at maxWidth and emits one line per ~78 characters, so a single 400 KB image becomes several thousand lines — and the request that caused them has scrolled away long before the body finishes.

trimBase64 collapses those runs:

ApiServices.init(const ApiConfig(
  logging: ApiLogOptions(trimBase64: true),
));
║      "caseNo": "FIR-2026-000148-ISB",
║      "data": iVBORw0KGgoAAAANSUhEUg…
║      …[412903 base64 chars elided]
║      "capturedAt": "2026-08-13T09:14:22Z"

It filters lines, not fields — by the time the logger calls logPrint it has already flattened the body, so the test is a shape test: a run of the base64 alphabet long enough that it cannot be an id or a status. Everything else passes through byte for byte, so a detail string, a case number, and a stack trace all read exactly as they did.

Off by default, because it rewrites the log stream and that should be your choice.

If your API returns long reference codes that the default would swallow, or you want the trimmer in front of your own sink, build it yourself — logPrint is a sink, not a switch:

ApiServices.init(ApiConfig(
  logging: ApiLogOptions(
    logPrint: Base64LogTrimmer(sink: myLogger.debug, minRunLength: 60).call,
  ),
));
Parameter Default Description
sink required Where surviving lines go.
keptChars 24 Characters of the blob kept before the ellipsis.
minRunLength 40 Shortest base64 run treated as a blob rather than an id.

Give each client its own instance — a trimmer holds the running count of elided characters.


Testing #

package:baaba_api_handler/testing.dart ships an in-memory FakeApiServices, so you can test a repository or controller without mocking Dio:

import 'package:baaba_api_handler/testing.dart';

late FakeApiServices api;

setUp(() {
  api = FakeApiServices();
  api.stubJson(HttpMethod.get, '/users/1', {'id': 1, 'name': 'Ada'});
});

test('loads the user', () async {
  final user = await UserRepository(api).find(1);

  expect(user.name, 'Ada');
  expect(api.recordedCalls.single.endpoint, '/users/1');
});

test('surfaces a validation error', () async {
  api.stubFailure(
    HttpMethod.post,
    '/users',
    statusCode: 422,
    body: {'errors': {'email': ['already taken']}},
  );

  final result = await api.post(endpoint: '/users', data: {...});

  result.fold(
    (f) => expect(f.validationErrors!['email'], ['already taken']),
    (_) => fail('expected a failure'),
  );
});
Method Purpose
stubJson(method, endpoint, body) Queue a response. A non-2xx statusCode becomes a failure, as it does in production.
stubFailure(method, endpoint, ...) Queue a failure, from a Failure or a status/body — built exactly as the real client builds one, so a 401 is ErrorSource.unauthorized carrying the body's message.
stubOffline(method, endpoint) Queue the no-connection short-circuit.
stub(method, endpoint, either) Queue a raw Either for full control.
stubDownload(endpoint) Queue a successful download. The savePath is recorded as the call's data.
recordedCalls Every call made, with endpoint, body, params, headers, files.
cancellations / cancelledTags Reasons and tags passed to cancelRequest.
reset() Clear stubs and history.

Stub the same endpoint more than once to script a sequence — each call consumes one entry and the last repeats, which makes "fails, then succeeds on retry" easy to set up. Calling an endpoint you didn't stub throws a StateError naming it, so an unexpected call fails loudly instead of quietly returning null.

The fake honours the parts of ApiConfig that shape an outcome, the same way the real client does: isSuccess (except on head and download), an already-cancelled CancelToken, and the observer — onRequest for every call that would have reached the network, then exactly one of onResponse or onFailure (both, for a *As<T> parse failure). Test your observer wiring through it with ApiServices.init(ApiConfig(observer: ...)).

Add ApiServices.reset() to tearDown so configuration doesn't leak between tests.


Configuration reference #

Every field on ApiConfig:

Field Default Description
baseUrl null Prefix for relative endpoints.
connectTimeout 30s Wait for the connection to be established.
receiveTimeout 30s Wait between response chunks.
sendTimeout 30s Wait while sending the body.
defaultHeaders {} Merged into every request.
bypassConnectivityCheck false Skip the pre-flight internet check — needed behind proxies that block the probe.
connectivityCacheTtl 5s How long a positive connectivity result is reused.
connectivityProbe null Replaces the default third-party ping with your own check.
logging ApiLogOptions() Console logger settings.
retry RetryPolicy() How transient failures are retried.
isSuccess null Reject a 2xx whose body says otherwise.
onRejected null Builds the Failure for a response isSuccess rejected.
mapFailure null Rewrites any Failure before the caller sees it.
httpClientAdapter null Certificate pinning, proxies.
observer null Request/response/failure hook.
interceptors [] Your own Dio interceptors, inserted after auth and before retry.
maxConcurrentRequests null Cap on requests in flight at once. Unlimited when unset; must be positive. A token refresh made through the client is never queued behind it.
auth null Token auth and refresh.

Settings read at request time take effect immediately: bypassConnectivityCheck, isSuccess, onRejected, mapFailure, and the observer's onResponse/onFailure. The rest are fixed when init builds the client, so changing them means calling init again: baseUrl, timeouts, defaultHeaders, logging, retry, auth, interceptors, httpClientAdapter, connectivityProbe, connectivityCacheTtl, maxConcurrentRequests, and the observer's onRequest.


License #

See LICENSE.

0
likes
150
points
417
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A typed, functional HTTP client for Flutter. Wraps Dio with Either-based error handling, token refresh, retries, request de-duplication, and a built-in test double.

Topics

#http #networking #dio #rest #api

License

BSD-3-Clause (license)

Dependencies

dio, equatable, flutter, fpdart, internet_connection_checker_plus, pretty_dio_logger

More

Packages that depend on baaba_api_handler