uploadMultipleFiles method

Future<List<String>> uploadMultipleFiles({
  1. required String url,
  2. required List<String> filePaths,
  3. String fileKey = 'files',
  4. Map<String, dynamic>? formData,
  5. Map<String, dynamic>? headers,
  6. bool useWithoutToken = false,
  7. bool cacheResponse = false,
  8. bool forceOffline = false,
  9. CancelToken? cancelToken,
  10. void onSendProgress(
    1. int,
    2. int
    )?,
  11. void onProgressPercentage(
    1. 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);
  }
}