multipart method

Future multipart(
  1. String endpoint,
  2. FormData data, {
  3. Map<String, dynamic> query = const {},
  4. CancelToken? cancelToken,
  5. Options? options,
  6. dynamic onSendProgress(
    1. int,
    2. int
    )?,
  7. dynamic onReceiveProgress(
    1. int,
    2. int
    )?,
})

MULTIPART: Sends a multipart/form-data POST request to the specified endpoint with files.

Returns the response data on success, or an error message if the request fails.

endpoint: The API endpoint to send the multipart request to. files: A map of field names to file paths for single file uploads. fileMapList: A map of field names to lists of file paths for multiple file uploads.

Implementation

Future<dynamic> multipart(
  String endpoint,
  FormData data, {
  Map<String, dynamic> query = const {},
  CancelToken? cancelToken,
  Options? options,
  Function(int, int)? onSendProgress,
  Function(int, int)? onReceiveProgress,
}) async {
  try {
    final response = await _dio.post(
      endpoint,
      data: data,
      queryParameters: query,
      cancelToken: cancelToken,
      options: options ?? Options(contentType: "multipart/form-data"),
      onSendProgress: onSendProgress,
      onReceiveProgress: onReceiveProgress,
    );
    if (isApiLoggerEnable) {
      await _sqfLiteService.add(data: {
        "url": response.realUri.toString(),
        "request": jsonEncode(response.requestOptions.data),
        "response": jsonEncode(response.data),
        "statusCode": response.statusCode,
        "error": "No Error",
        "header": jsonEncode(response.headers.map),
        "method": response.requestOptions.method,
        "timestamp" : DateTime.now().toIso8601String(),
      });
    }
    return response.data;
  } catch (e) {
    _handleError(e);
  }
}