Search code examples
flutterdartdio

How can i post image with dio in flutter


i want to send image to server by jpg format by dio package , how can i do this ?

choose image method :

  void _chooseImageCamera() async {
file = await ImagePicker.pickImage(source: ImageSource.camera,imageQuality: 50);
setState(() {
  file = file;
  print(file);
});

upload image method :

  void _upload() async {
if (file == null) return;
String fileName = file.path.split('/').last;
Map<String, dynamic> formData = {
  "image": await MultipartFile.fromFile(file.path,filename: fileName),
};
await serverRequest().getRequest("/Information", formData).then((onValue) {
  print(json.decode(onValue));
});

Anyone help me ?

thanks


Solution

  • Please try this

           Future<dynamic> _upload() async {
                if (file == null) return;
                String fileName = file.path.split('/').last;
                Map<String, dynamic> formData = {
                  "image": await MultipartFile.fromFile(file.path,filename: fileName),
                };
               return await Dio()
            .post(url,data:formData).
        then((dynamic result){
        print(result.toString());
    });
    }