1 year ago

#270760

test-img

FadyFouad

Flutter Http Multi part Request get response body

I want to get response data from MultipartRequest I uploaded an image to service and I should get image URL in response but I got an Bad state: Stream has already been listened to.

this My code snippet

  //upload image
  Future<ApiResponse> uploadOrderPic(
      {required String orderID,
      required File image,
      required String type}) async {
    ApiResponse apiResponse = ApiResponse();
    var token = await getToken();
    try {
      var request = http.MultipartRequest(
          "POST", Uri.parse(uploadPicUrl));
      request.files
          .add(await http.MultipartFile.fromPath('image', image.path));
      request.fields['id'] = '1084';
      request.fields['type'] = 'invoice';
      request.headers['${HttpHeaders.authorizationHeader}'] = 'Bearer $token';
      request.send().then((value) => http.Response.fromStream(value).then((onValue) {
        try {
          // get response...
          logger.i(value.stream.bytesToString());

        } catch (e) {
          // handle exception
          logger.e(e);// This catch Stream has already been listened to.
        }
      }));
    } catch (e) {
      logger.e(e);
      apiResponse.error = serverError;
    }
    return apiResponse;
  }

the logger.i(value.statusCode); print status code is 200(ok) but logger.i(value.stream.bytesToString()); print exception

flutter

http

multipartfile

0 Answers

Your Answer

Accepted video resources