1

I have to upload image from gallery to server using provider in Flutter.

Here is the file picker

  _loadPicker(ImageSource source) async {
   File picked = await ImagePicker.pickImage(source: ImageSource.gallery);
   print(picked);
   if (picked != null) {
   final response = await Provider.of<ProfilePictureUpdate>(context, listen: 
    false).profilePicUpdate(picked);
    if (response["status"] ) {
      Fluttertoast.showToast(msg: response["title"]);
     }
      else {
     Fluttertoast.showToast(msg: response["title"]);
      }
     }
    }

And here is the post method

 Future<Map<String, dynamic>> profilePicUpdate(picked) async {
 try {
   final response = await ApiRequest.send(route: "profile/update/picture", method: "POST", 
   body: {
    "  photo_url" : picked,
    });
  if (response.statusCode == 200 ) {
    return {
      "status": true,
      "title" : response["title"]
    };
  }
  }
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Aravind B
  • 87
  • 1
  • 3

1 Answers1

0

If you want sent image to you have to use formData( multi part) in 'Dio' similar web (enctype). In http, you can also use multipart.

Must remember u use image is always not same, here use this field when server side params name same.

 class ImageRepository {
  Future<dynamic> uploadImage(filepath) async {
  FormData formData = FormData.fromMap({
    "image": await MultipartFile.fromFile(filepath,
      filename: filepath.split('/').last)
   });
   var response = await Dio().post(
    url,
    data: formData),
  );
  print(response.data);
    if (response.statusCode == 200) {
    return 'Image Upload';
  } else {
    throw Exception 'Problem occour';
  }
  }