MultipartFile.fromString constructor

MultipartFile.fromString(
  1. String field,
  2. String value,
  3. {String? filename,
  4. MediaType? contentType}
)

Creates a new MultipartFile from a string.

The encoding to use when translating value into bytes is taken from contentType if it has a charset set. Otherwise, it defaults to UTF-8. contentType currently defaults to text/plain; charset=utf-8, but in the future may be inferred from filename.

Implementation

factory MultipartFile.fromString(String field, String value,
    {String? filename, MediaType? contentType}) {
  contentType ??= MediaType('text', 'plain');
  var encoding = encodingForCharset(contentType.parameters['charset'], utf8);
  contentType = contentType.change(parameters: {'charset': encoding.name});

  return MultipartFile.fromBytes(field, encoding.encode(value),
      filename: filename, contentType: contentType);
}