addInterceptor method
Adds an interceptor to the adapter.
Interceptors can modify requests before they are sent, modify responses before they are returned, or handle errors.
Parameters:
interceptor: The interceptor to add
Interceptors are executed in the order they are added.
Example:
class AuthInterceptor extends AdapterInterceptor {
@override
Future<void> onRequest(
AdapterRequest request,
RequestInterceptorHandler handler,
) async {
final modifiedRequest = request.copyWith(
headers: {...request.headers, 'Authorization': 'Bearer token'},
);
handler.next(modifiedRequest);
}
}
adapter.addInterceptor(AuthInterceptor());
See also:
- AdapterInterceptor for interceptor implementation details
- removeInterceptor to remove an interceptor
Implementation
@override
void addInterceptor(AdapterInterceptor interceptor) {
_interceptors.add(interceptor);
}