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;
}
...
}
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{
...
}
Edit:
if you are passing image to a json then you missing out is data:image/png;base64 something like this lets assume this function return string.
///include import path
import 'package:path/path.dart' as path;
///////////////////////////////////////
imagetobase64(String? imagePath){
final extension = path.extension(imagePath
.substring(imagePath.lastIndexOf("/"))
.replaceAll("/", ""));
//// the extension return is png or jpg or jpeg which is needed
final bytes = File(imagePath).readAsBytesSync();
String base64 =
"data:image/${extension.replaceAll(".", "")};base64,${base64Encode(bytes)}";
return base64;
}
uploadimage is of type XFile? and you are passing it to a function which accepts parameter of File? type
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.