MultipartFile.fromString constructor

MultipartFile.fromString(
  1. String value, {
  2. String? filename,
  3. DioMediaType? contentType,
  4. Map<String, List<String>>? headers,
})

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 value, {
  String? filename,
  DioMediaType? contentType,
  final Map<String, List<String>>? headers,
}) {
  contentType ??= MediaType('text', 'plain');
  final encoding = encodingForCharset(
    contentType.parameters['charset'],
    utf8,
  );
  contentType = contentType.change(parameters: {'charset': encoding.name});

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