For the complete documentation index, see llms.txt. This page is also available as Markdown.

Interceptors

Request

Implement Interceptor class.

Request interceptors are called just before sending a request.

class MyRequestInterceptor implements Interceptor {
  MyRequestInterceptor(this.token);

  final String token;

  @override
  FutureOr<Response<BodyType>> intercept<BodyType>(Chain<BodyType> chain) async {
    final request = applyHeader(chain.request, 'auth_token', 'Bearer $token');
    return chain.proceed(request);
  }
}

Response

Implement Interceptor class.

Called after successful or failed request.

Breaking out of an interceptor

In some cases you may run into a case where it's not possible to continue within an interceptor and want to break out/cancel the request. This can be achieved by throwing an exception. This will not return a response and the request will not be executed.

Keep in mind that when throwing an exception you also need to handle/catch the exception in calling code.

For example, if you want to stop the request if the token is expired:

It is not strictly necessary to throw an exception in order to break out of the interceptor. Other approaches can also be used depending on how the project is structured, such as calling an injected service or providing a callback that handles the state of the app.

Builtins

Both the CurlInterceptor and HttpLoggingInterceptor use the Dart logging package. To see logging in the console, the logging package also needs to be added to your project and configured.

For example:

Last updated

Was this helpful?