Is Flutter http.MultipartRequest support boolean?

42 Views Asked by At

In Flutter, I am using http.MultipartRequest for POST image and sending the payload into backend. Some of my payload is boolean but http.MultipartRequest didn't support boolean. How can I solve this problem?

This is my approach.

  String url,
  Map<String, dynamic> payload,
  File? imageFile,
) async {
  final chatReplyData =
      ChatReplyData(responseStatus: 0, validStatusCode: false);

  try {
    final request = http.MultipartRequest(
      'POST',
      Uri.parse('${getApiPrefix()}$url'),
    );

    // Add image file to the request
    if (imageFile != null) {
      final imageStream = http.ByteStream(imageFile.openRead());
      final imageLength = await imageFile.length();
      final multipartFile = http.MultipartFile(
        'file',
        imageStream,
        imageLength,
        filename: imageFile.path,
      );
      request.files.add(multipartFile);
    }

    // Add other payload data
    payload.forEach((String key, dynamic value) {
      request.fields[key] = value.toString();
    });

    final response = await request.send();

    // get response as json string
    final responseData = await response.stream.bytesToString();
    // Parse response as JSON
    final responseBody = jsonDecode(responseData);

    final responseStatus = response.statusCode;
    chatReplyData.responseStatus = responseStatus;

    debugPrint(
        'myLog: getChatReply -> url = $url, payload = $payload, imageFile = $imageFile');
    debugPrint(
        'myLog: getChatReply -> responseStatus = $responseStatus, responseBody = $responseBody');

    chatReplyData.message = responseBody?['message'];

    return chatReplyData;
  } catch (e) {
    debugPrint('myError: getChatReply -> error = $e');
    return chatReplyData;
  }
}

Here

payload.forEach((String key, dynamic value) {
      request.fields[key] = value.toString();
    });

I don't want to pass all fields as a String. Cause some of my value is boolean.

1

There are 1 best solutions below

1
SYED MOHAMED RIBAS On

You try this method it will be working fine.

#API Definition

Future multipartRequest(
    url,
    payload,
    imageFile,
  ) async {
    String baseUrl = getBaseUrl();

    var request = http.MultipartRequest('POST', Uri.parse('$baseUrl$url'));
    request.fields.addAll(payload);

    if (imageFile.isNotEmpty) {
      for (var image in imageFile) {
        var file = await http.MultipartFile.fromPath('FileList', image);
        request.files.add(file);
      }
    }

    try {
      var response = await request.send();
      if (response.statusCode == 200) {
        return {
          "status": response.statusCode,
          "data": await response.stream.bytesToString()
        };
      } else {
        return {"status": response.statusCode, "data": response.reasonPhrase};
      }
    } catch (e) {
      return e;
    }
  }

#API Call

multipartRequest("endpoint url",
   {
          "payload1":"Something",
          "payload2":24,
          "payload3":false,

        },
        imagefiles);