onRequest method
Called when the request is about to be sent.
Implementation
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// Verify that the authentication bloc is available
if (GetIt.instance.isRegistered<AuthenticationBloc>()) {
final authenticationBloc = GetIt.instance.get<AuthenticationBloc>();
// Only proceed if the user is authenticated
if (authenticationBloc.state.isAuthenticated) {
try {
// Extract the language code from the authenticated user's profile
final language = (authenticationBloc.state
as UserAuthenticatedWithSelectedTenantState)
.user
.language
.value
.code
.value;
// Add language header only if the code is valid
if (language.isNotEmpty) {
options.headers['X-Language'] = language;
}
} catch (e) {
// Silently handle any errors in language extraction
// to prevent disrupting the API call
}
}
}
// Continue with the request
handler.next(options);
}