5

I am trying to make a put request to update a user profile with GetConnect. The user profile takes some normal JSON fields and a MultipartFile for the profile picture.

Here's my ProfileProvider class:

class ProfileProvider extends GetConnect {
 Future<ProfileModel> updateProfile({
  String name,
  String email,
  String address,
  File avatar,
 }) async {

 final headers = {
  "Authorization": "Bearer $token",
  "Content-Type": "application/json",
  "Accept": "application/json",
};

String fileName = avatar.path.split("/").last;
final form = FormData({
  "name": name,
  "email": email,
  "address": address,
  "avatar": MultipartFile(avatar, filename: fileName),
});

final response = await put(url, form, headers: headers);

if (response.statusCode == 200) {
  final profileModel = ProfileModel.fromJson(response.bodyString);

  return profileModel;
   }
  }
 }

As you can see I am using FormData to send the encoded object to the API. But the response says the field name, email, address is required but I am already sending them with the form. What I am doing wrong here? The GetConnect documentation has a similar file upload doc but without additional fields (ie. name, email, address). Also if I omit the MultiartFile from the FormData, it's working.

S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30

1 Answers1

0

The issue is the type of your body. Remove the 'FormData'
Try the code below instead.

final form = {
  "name": name,
  "email": email,
  "address": address,
  "avatar": MultipartFile(avatar, filename: fileName),
};