create method

Future<CreatedWithPublicIdAndLink> create({
  1. required String body,
  2. String? title,
  3. PostType postType = PostType.text,
  4. bool allowComments = true,
  5. bool isDraft = false,
  6. FilePayload? file,
})

Implementation

Future<CreatedWithPublicIdAndLink> create({
  required String body,
  String? title,
  PostType postType = PostType.text,
  bool allowComments = true,
  bool isDraft = false,
  FilePayload? file,
}) async {
  final uri = Uri.parse('${_client.baseURL}/api/v1/client/micropost');
  final request = http.MultipartRequest('POST', uri);

  request.fields['body'] = body;
  request.fields['post_type'] = postType.value;
  request.fields['allow_comments'] = allowComments.toString();
  request.fields['is_draft'] = isDraft.toString();

  if (title != null) request.fields['title'] = title;

  if (file != null) {
    request.files.add(
      http.MultipartFile.fromBytes(
        'file',
        file.bytes,
        filename: file.filename,
      ),
    );
  }

  final response = await _client.send(request);
  return CreatedWithPublicIdAndLink.fromJson(
    jsonDecode(response.body),
  );
}