Reputation: 43
I have updated the image_picker library in flutter to the latest version and when implementing the below code I am getting the error
CODE
File file = await ImagePicker.getImage(
source: ImageSource.camera,
maxHeight: 675,
maxWidth: 960,
);
ERROR
error: Instance member 'getImage' can't be accessed using static access.
error: A value of type 'PickedFile' can't be assigned to a variable of type 'File'.
please guide me to resolve this
Upvotes: 1
Views: 2611
Reputation: 801
If you want to work with your above code you have to change the package version
change the dependencies in pubspec.yaml file
image_picker: ^0.6.0
Upvotes: 0
Reputation: 2327
Intstall image picker and setup correctly
Initialize variables
File _image;
final Imagepicker = ImagePicker();
Button for opening bottom model
ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.red, // background
onPrimary: Colors.white, // foreground
),
onPressed: () {
openImagePickerModal(context);},
child: Text('Select Image'),
),
To view selected Image
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 200,
width: 200,
child: Image.file(
_image,
fit: BoxFit.fill,
)),
),
Image Picker Model Bottom Sheet,For Select Images or Capture Image
void openImagePickerModal(BuildContext context) {
final flatButtonColor = Theme.of(context).primaryColor;
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 180.0,
padding: EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
'Pick An Image',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
SizedBox(
height: 15.0,
),
FlatButton(
textColor: flatButtonColor,
child: Text(
'Use Camera',
style: TextStyle(fontSize: 15),
),
onPressed: () {
Pickimage(context, ImageSource.camera);
Navigator.pop(context);
},
),
FlatButton(
textColor: flatButtonColor,
child: Text(
'Use Gallery',
style: TextStyle(fontSize: 15),
),
onPressed: () {
Pickimage(context, ImageSource.gallery);
Navigator.pop(context);
},
),
],
),
);
});
}
Function for Selecting Image from gallery or camera Image
Future Pickimage(BuildContext context, ImageSource source) async {
if (source != null) {
final pickedFile = await ImagePicker.pickImage(source: source);
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
} else {
print('No image selected.');
}
});
} else {}
}
Upvotes: 0
Reputation: 994
As mentioned in the error message ImagePicker.getImage()
return a PickedFile
You can convert it to File
type like this:
PickedFile pickedFile = await ImagePicker.getImage(
source: ImageSource.camera,
maxHeight: 675,
maxWidth: 960,
);
File imageFile = File(pickedFile.path);
Upvotes: 5