uploading file to server in flutter

I want to upload file to server for removing background using api.But the app freeze when I upload image using multipart request and using simple http request gives “Error in removeBg: type ‘MultipartFile’ is not a subtype of type ‘String’ in type cast”.I have also tried using dio but the app again freeze.
When debugging,it prints log until request is send.

code:

Future<void> removeBg() async {
    String uri = "https://api.remove.bg/v1.0/removebg";
    Map<String, String> headers = {
      'X-API-Key': "t1689LyUTModrnaHgKRjsJuk",
    };

    try {
     
      http.Response response = await http.post(
        Uri.parse(uri),
        headers: headers,
        body: {
          "image_file": http.MultipartFile(
              filename: image!.path.split("/").last,
              "image_file",
              image!.readAsBytes().asStream(),
              image!.lengthSync())
        },
      );

      if (response.statusCode == 200) {
        setState(() {
          _isLoaded = true;
          responseImage = response.bodyBytes;
        });
      } else {
        print("API request failed with status code: ${response.statusCode}");
      }
    } catch (e) {
      print("Error in removeBg: $e");
    }
  }

using multipart request

    Future<void> removeBg() async {
        String uri = "https://api.remove.bg/v1.0/removebg";
        Map<String, String> headers = {
          'X-API-Key': "t1689LyUTModrnaHgKRjsJuk",
        };
    
          try {
       
          final request = http.MultipartRequest("POST", Uri.parse(uri))
            ..files.add(http.MultipartFile(
                filename: image!.path.split("/").last,
                "image_file",
                image!.readAsBytes().asStream(),
                image!.lengthSync()));
          request.headers.addAll(headers);
    
          await request.send().then((response) async {
         
            response.stream.transform(utf8.decoder).listen((value) {
              print(value);
            });
          }).catchError((e) {
            print(e);
          });
        } catch (e) {
          print("Error in removeBg: $e");
        }
      }

  

  • Instead of directly passing the MultipartFile object, utilize http.MultipartFile.fromBytes(). This might help you.

    – 




  • ok,i will try with this

    – 

  • does not work ,still app freezes

    – 

  • Okay, it will post a solution based on this http.MultipartFile.fromBytes(). Try with that.

    – 

Try with this modified solution using http.MultipartFile.fromBytes(). I hope this will help you out.

 Future<void> removeBg() async {
      String uri = "https://api.remove.bg/v1.0/removebg";
      Map<String, String> headers = {
        'X-API-Key': "t1689LyUTModrnaHgKRjsJuk",
      };
    
      try {
        var request = http.MultipartRequest('POST', Uri.parse(uri));
        request.headers.addAll(headers);
        request.files.add(await http.MultipartFile.fromPath(
            'image_file', image!.path)); // Here, image!.path is the path to your image file
        var response = await request.send();
        
        if (response.statusCode == 200) {
          setState(() {
            _isLoaded = true;
            responseImage = await response.stream.toBytes();
          });
        } else {
          print("API request failed with status code: ${response.statusCode}");
        }
      } catch (e) {
        print("Error in removeBg: $e");
      }
    }

Leave a Comment