I want upload image from gallery to api here is my image api
Future profileImageUser(String user_id, String image) async {
String url = 'api';
final response = await http.post(Uri.parse(url),
body: {
'user_id': user_id,
'image': image
}
);
var convertedDataJson = jsonDecode(response.body);
return convertedDataJson;
}
Here is how i am getting user id and image to upload it through rest api
ImagePicker picker = ImagePicker();
Future _imgFromCamera() async {
final image = await ImagePicker().pickImage(source: ImageSource.camera);
setState(() {
_image = image;
final bytes = File(_image!.path).readAsBytesSync();
String img64 = base64Encode(bytes);
print(img64);
});
SharedPreferences prefs = await SharedPreferences.getInstance();
if (_formKey.currentState!.validate()) {
var myProfile = _image;
print(myProfile);
var myId = prefs.getString('user_id');
print(myId);
var rsp = await profileImageUser(
myProfile.toString(),
myId.toString(),
);
print(rsp);
if (rsp['status'] == true) {
print(rsp);
} else {
print('failed');
}
}
}
when i upload image from gallery or camera it print id and image as follow
I/flutter ( 6665): Instance of 'XFile'
I/flutter ( 6665): 111
but it also show error that user doest not exist
I/flutter ( 6665): {status: false, message: User Does Not Exists!, data: null}
kindly provide a solution.