uploadMultipleFiles method
Future<List<String> >
uploadMultipleFiles({
- required String url,
- required List<
String> filePaths, - String fileKey = 'files',
- Map<
String, dynamic> ? formData, - Map<
String, dynamic> ? headers, - bool useWithoutToken = false,
- bool cacheResponse = false,
- bool forceOffline = false,
- CancelToken? cancelToken,
- void onSendProgress()?,
- void onProgressPercentage(
- double progress
Implementation
Future<List<String>> uploadMultipleFiles({
required String url,
required List<String> filePaths,
String fileKey = 'files',
Map<String, dynamic>? formData,
Map<String, dynamic>? headers,
bool useWithoutToken = false,
bool cacheResponse = false,
bool forceOffline = false,
CancelToken? cancelToken,
void Function(int, int)? onSendProgress,
void Function(double progress)? onProgressPercentage,
}) async {
try {
if (forceOffline) {
throw {'error': 'File upload requires internet connection'};
}
final formDataMap = FormData.fromMap({
...?formData,
fileKey: filePaths.map((path) => MultipartFile.fromFileSync(path)).toList(),
});
// Wrap progress callback to include percentage
void Function(int, int)? wrappedOnSendProgress;
if (onSendProgress != null || onProgressPercentage != null) {
wrappedOnSendProgress = (sent, total) {
onSendProgress?.call(sent, total);
if (total > 0) {
final percentage = (sent / total) * 100;
onProgressPercentage?.call(percentage);
}
};
}
final response = await _dio.post(
url,
data: formDataMap,
options: Options(
headers: headers,
extra: {
'useWithoutToken': useWithoutToken,
'cacheResponse': cacheResponse,
},
),
cancelToken: cancelToken,
onSendProgress: wrappedOnSendProgress,
);
return List<String>.from(response.data['paths'] ?? response.data['urls']);
} on DioException catch (e) {
throw _handleDioError(e);
}
}