ApiObserver class abstract
A single place to watch every request the package makes — for crash reporting, analytics, or a debug overlay.
Without this, error reporting has to be duplicated inside every caller's
fold. Register one observer instead:
class SentryObserver extends ApiObserver {
@override
void onFailure(Failure failure, RequestOptions? options) {
Sentry.captureMessage(
'${options?.method} ${options?.path} -> ${failure.code.value}',
withScope: (scope) => scope.setContexts('api', {
'message': failure.message,
'body': failure.data,
}),
);
}
}
ApiServices.init(ApiConfig(observer: SentryObserver()));
Every method has a no-op default, so override only what you need.
Failures you would not otherwise see. onFailure fires for failures
Dio never produces an exception for — the offline short-circuit, response
parsing errors from the *As<T> methods, and bodies rejected by
ApiConfig.isSuccess. That is the main reason to observe here rather than
adding your own Dio interceptor.
Callbacks must not throw. They are invoked inside the request path and
wrapped in a try/catch, so a throwing observer cannot break a request —
but the error is swallowed, not reported. Keep the bodies cheap and
defensive; hand off anything slow to a queue.
Constructors
- ApiObserver()
-
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
onFailure(
Failure failure, RequestOptions? options) → void -
Fires once per call for every
Leftthe package returns, whatever produced it: an HTTP error, a timeout, the offline short-circuit, a rejectedisSuccessbody, or a parse failure. -
onRequest(
RequestOptions options) → void - Fires as a request leaves, after auth headers have been attached.
-
onResponse(
Response response) → void -
Fires once per call for each response handed back as
Right. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited