getFormData method

Map<String, dynamic> getFormData(
  1. dynamic data
)

Implementation

Map<String, dynamic> getFormData(data) {
  final List<FormDataFieldModel> fields = [];
  final List<FormDataFileModel> files = [];

  if (data.fields.isNotEmpty == true) {
    data.fields.forEach((entry) {
      fields.add(FormDataFieldModel(entry.key, entry.value));
    });
  }

  if (data.files.isNotEmpty == true) {
    data.files.forEach((entry) {
      files.add(
        FormDataFileModel(
          entry.value.filename,
          entry.value.contentType.toString(),
          entry.value.length,
        ),
      );
    });
  }

  final Map<String, dynamic> formData = {};
  for (var field in fields) {
    formData[field.name] = field.value;
  }

  List<Map<String, dynamic>> fileMaps = files.map((file) {
    return {
      'fileName': file.fileName,
      'contentType': file.contentType,
      'length': file.length,
    };
  }).toList();

  formData['files'] = fileMaps;

  return formData;
}