upload<T> method

Future<Either<BaseException, T>> upload<T>(
  1. String path, {
  2. required List<File> files,
  3. required T mapper(
    1. dynamic json
    ),
})

Implementation

Future<Either<BaseException, T>> upload<T>(
  String path, {
  required List<File> files,
  required T Function(dynamic json) mapper,
}) async {
  try {
    final data = FormData.fromMap({
      'files': await Future.wait(
        files.map(
          (file) => MultipartFile.fromFile(
            file.path,
          ),
        ),
      ),
    });

    final response = await _dio.post(
      path,
      data: data,
      options: Options(
        contentType: 'multipart/form-data',
      ),
    );

    return Right(mapper(response.data));
  } catch (err, s) {
    return ErrorHandler.fromError(err, stackTrace: s);
  }
}