Home > Blockchain >  how to pass xFile as a function parameter in flutter
how to pass xFile as a function parameter in flutter

Time:09-13

I want to upload images to the server using flutter and HTTP package. I am able to display user-selected images but I want to upload them to the server but when I try to pass the image file to the function it gives me an error.

Image Picker Code :

 XFile? uploadimage;
  final ImagePicker _picker = ImagePicker();
  Future<void> chooseImage() async {
    var chooseImage = await _picker.pickImage(source: ImageSource.gallery);
    setState(() {
      uploadimage = chooseImage;
    });
  }

**services file code **

 AdminSupervisorServices.createNewSupervisor(
                                    _nameController.text,
                                    _emailController.text,
                                    _addressController.text,
                                    _siteController.text,
                                    _mobileController.text,
                                    _passwordController.text,
                                    uploadimage // error here
)

function body

static createNewSupervisor(String name, String email, String address,
      String site, String mobileNumber, String password, File? image)async{
...
}

CodePudding user response:

uploadimage is of type XFile? and you are passing it to a function which accepts parameter of File? type

CodePudding user response:

try this
if this work

    import 'dart:io';
    /////// Import for File

     final File uploadimage = File("");
     final ImagePicker _picker = ImagePicker();
      
    Future<void> chooseImage() async {
        var chooseImage = await _picker.pickImage(source: ImageSource.gallery);
        setState(() {
          uploadimage = File(chooseImage.path);
        });
      }
    
    the function you have
    
    AdminSupervisorServices.createNewSupervisor(
                                    _nameController.text,
                                    _emailController.text,
                                    _addressController.text,
                                    _siteController.text,
                                    _mobileController.text,
                                    _passwordController.text,
                                    uploadimage 
);
    
    static createNewSupervisor(String name, String email, String address,
          String site, String mobileNumber, String password, File? image)async{
    ...
    } 
  • Related