Flutter Dio image upload

In many mobile apps it is common to have a profile picture. With that comes the requirement to let the user select a picture and upload it to server. In this post we will see how we can upload a image/file to remote server using dio library. Same code will work in non flutter environments as well.

Generally the file upload is done in two ways.

  • File will be a part form request along with other key-value data.
flutter image upload with form data
  • File itself is the body of post/put request. In this scenario there is no other data. The end point considers the request body as file data.
flutter image upload as post body

First lets see how these two scenarios plays out in postman

postman upload file with form data
postman upload file (image in this case)

Lets develop couple of methods for sending post request.

Lets test both the scenario by uploading an image to server . I will be using image_picker to let user select an image.

9 Comments

Add a Comment

Your email address will not be published. Required fields are marked *