timingInterceptor static method
Interceptor
timingInterceptor({})
Creates a timing interceptor that logs request duration
Implementation
static Interceptor timingInterceptor({
String prefix = '[Google Maps API]',
Duration? slowRequestThreshold,
}) {
return InterceptorsWrapper(
onRequest: (options, handler) {
options.extra['startTime'] = DateTime.now().millisecondsSinceEpoch;
handler.next(options);
},
onResponse: (response, handler) {
final startTime = response.requestOptions.extra['startTime'] as int?;
if (startTime != null) {
final duration = DateTime.now().millisecondsSinceEpoch - startTime;
final durationMs = Duration(milliseconds: duration);
if (slowRequestThreshold == null ||
durationMs > slowRequestThreshold) {
print('⏱️ $prefix Request took ${durationMs.inMilliseconds}ms');
}
}
handler.next(response);
},
onError: (error, handler) {
final startTime = error.requestOptions.extra['startTime'] as int?;
if (startTime != null) {
final duration = DateTime.now().millisecondsSinceEpoch - startTime;
final durationMs = Duration(milliseconds: duration);
print(
'⏱️ $prefix Failed request took ${durationMs.inMilliseconds}ms',
);
}
handler.next(error);
},
);
}