Home > Software engineering >  ImagePicker image not diplayed after selection /Flutter
ImagePicker image not diplayed after selection /Flutter

Time:09-27

Used ImagePicker to select an image in gallery. Worked but no image is desplayed. ERROR : Unhandled Exception: type 'XFile' is not a subtype of type 'File' in type cast

Below my code :

class _RegisterScreenState extends State {
File image;
String imgUrl;
final imagePicker = ImagePicker();

Future getImage() async {

File img = await imagePicker.pickImage(
source: ImageSource.gallery);
setState(() {
image = img as File;
});
}

child: CircleAvatar(
radius: 60,
backgroundColor: Colors.white,
backgroundImage: image != null
? Image.file(image)
: NetworkImage(
"https://cdn-icons-png.flaticon.com/512/1177/1177568.png"),
),

ERROR : Unhandled Exception: type 'XFile' is not a subtype of type 'File' in type cast

I am on :

version: 1.0.0 1
environment:
sdk: ">=2.7.0 <3.0.0"
image_picker: ^0.8.4 2
path_provider: ^2.0.5

CodePudding user response:

I too faced this error. I solved this by:

File myImage;
Future getImage() async {
   File img = await ImagePicker.pickImage(source: ImageSource.gallery);      
   setState(() {
      myImage = File(img.path);
   });
}

CodePudding user response:

Using image_picker: ^0.7.4 and try:

  ImagePicker imagePicker = ImagePicker();
  final image = await imagePicker.getImage(source: ImageSource.gallery);

You will get the photo by using image.path.

  • Related