I am using the Flutter Plugin Image_picker to choose images so that I want to upload image after selected the image

Viewed 20

this is my code

Future<File> _imageFile;
    
     void _onImageButtonPressed(ImageSource source) async {
      setState(() {
       _imageFile = ImagePicker.pickImage(source: source);
      });
    }

I find this code in flutter documentation but its not work

var uri = Uri.parse("http://pub.dartlang.org/packages/create");
var request = new http.MultipartRequest("POST", url);
request.fields['user'] = 'nweiz@google.com';
request.files.add(new http.MultipartFile.fromFile(
    'package',
    new File('build/package.tar.gz'),
    contentType: new MediaType('application', 'x-tar'));
request.send().then((response) {
  if (response.statusCode == 200) print("Uploaded!");
});
1 Answers

I used file_picker library to pick files. you can use this for pick images as well.

Future getPdfAndUpload(int position) async {

    File file = await FilePicker.getFile(
      type: FileType.custom,
      allowedExtensions: ['pdf','docx'], //here you can add any of extention what you need to pick
    );

    if(file != null) {

      setState(() {

          file1 = file; //file1 is a global variable which i created
     
      });

    }
  }
Related