title: Post form data to server flutter
body: <p>I have been having a lot of trouble sending a <code>post</code> request to a server. It  expects a <code>form data</code> type.</p>
  
  <p>This is the error I get after my input.</p>
  
  <pre><code>  `image: [The image must be an image.]}}
  </code></pre>
  
  <p>Most of my data are strings except for an <code>int</code> and a <code>file Image</code> which is selected from gallery by user.</p>
  
  <p>This is my code:</p>
  
  <p><strong>dart code</strong></p>
  
  <pre><code>if(_image!=null){
        setState(() {
          _isLoading = true;
        });
          SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
          var uri = NetworkUtils.host +
              AuthUtils.updateSessionRequest;
          Map&lt;String, String&gt; data = {"_method": "PATCH",
            "first_name": widget.first_name,
            "last_name": widget.last_name,
            "phone": widget.phone,
          "industry":widget.industry,
          "country": widget.country,
          "state": widget.state,
          "fav_quote": widget.fav_quote,
          "bio_interest": widget.bio_text,
          "terms": "1",
          "company": widget.company,
          "position": widget.job_position,
          "linked_in":widget.linkedin_profile,
          "institution": widget.institution,
          "degree": widget.degree,
          "preference[0]": widget.industry};
          String authToken = sharedPreferences.getString("token");
          try {
            final response = await http.post(
              uri,
              body: data,
              headers: {
                'Accept': 'application/json',
                'Authorization': 'Bearer ' + authToken,
              },
            );
  
            final responseJson = json.decode(response.body);
            print(responseJson.toString());
            if (response.statusCode == 200 || response.statusCode == 201) { 
         //upload image to server after success response
         uploadImage(_image);
                NetworkUtils.showToast("Profile successfully update!");
              });
            } else{
              setState(() {
                _isLoading = false;
              });
              NetworkUtils.showSnackBar(_scaffoldKey, 'An error occurred. Please try again');
            }
            return responseJson;
          } catch (exception) {
            print(exception.toString());
            setState(() {
              _isLoading = false;
            });
            NetworkUtils.showSnackBar(_scaffoldKey, 'An error occurred. Please try again');
          }
      } 
  
   uploadImage(File image) async{
      var request = http.MultipartRequest(
          "POST",
          Uri.parse(NetworkUtils.host +
              AuthUtils.endPointUpdateProfile));
      request.files.add(await http.MultipartFile.fromPath(
        'image',
        image.path,
      ));
  
      try {
      var streamedResponse = await request.send();
      var response = http.Response.fromStream(streamedResponse);
      return response;
      } catch (e) {
      rethrow;
      }
    }
    }
  </code></pre>