onResponse method
Logs the received response unless requestOptions.extra['logResponse']
is false.
Emits the status code, method, URI, and response body. Uses 'error'
level for HTTP 4xx/5xx responses and 'info' for all other status
codes, then forwards the response to the next handler.
Implementation
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
if (response.requestOptions.extra['logResponse'] == false) {
return super.onResponse(response, handler);
}
final buffer = StringBuffer();
buffer.writeln('RESPONSE');
buffer.writeln(
'[${response.statusCode}] ${response.requestOptions.method} ${response.requestOptions.uri}');
buffer.writeln('Data: ${response.data}');
onLog(
message: buffer.toString(),
level: (response.statusCode ?? 0) >= 400 ? 'error' : 'info',
context: 'Lucky',
);
super.onResponse(response, handler);
}