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 {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<int> imageBytes = image!.readAsBytesSync();
String baseimage = base64Encode(imageBytes);
var token = prefs.getString("token");
var response = await http
.post(Uri.parse("$baseURL/api/mmmmmmmm"), headers: {
'Authorization': 'Bearer $token',
}, body: {
"full_name": name,
"address": address,
"mobile_no": mobileNumber,
"email": email,
"site_name": site,
"password": password,
"image": baseimage,
});
print(response.body.toString());
var data = jsonDecode(response.body);
return data;
}
...
}