onRequest method
Called when the request is about to be sent.
Implementation
@override
void onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
// Already FormData - just ensure content type
if (options.data is FormData) {
options.contentType ??= 'multipart/form-data';
handler.next(options);
return;
}
// Check for files in Map data
if (options.data is Map<String, dynamic>) {
final data = options.data as Map<String, dynamic>;
if (_containsFiles(data)) {
final formData = await _toFormData(data);
options.data = formData;
options.contentType = 'multipart/form-data';
handler.next(options);
return;
}
}
// Apply default content type for non-null data (if not already set)
if (options.contentType == null &&
options.data != null &&
defaultContentType != null) {
options.contentType = defaultContentType;
}
handler.next(options);
}