Home > Software design >  Unhandled Exception: type 'XFile' is not a subtype of type 'PickedFile' in type
Unhandled Exception: type 'XFile' is not a subtype of type 'PickedFile' in type

Time:11-21

I'm trying to make it where the user is able to change their profile pic inside the app. I'm able to pull the camera and take a picture in flutter. However, the image is not being saved, and inside visual studio code I get the following error: [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'XFile' is not a subtype of type 'PickedFile' in type cast

Here are the variables I'm using:

PickedFile? _imageFile;
final ImagePicker _picker = ImagePicker();

Here is the function where they're being used:

void takePhoto(ImageSource source) async {
    final pickedFile = await _picker.pickImage(source: source);
    setState(() {
      _imageFile = pickedFile as PickedFile;
    });
  }

Also, I'm using the latest version of image picker, which is image_picker: ^0.8.4 4.

CodePudding user response:

you can try change PickedFile -> XFile ? because you are using new version. And now they use XFile instead of PickedFile. https://github.com/flutter/plugins/blob/55e246bfa0fd43ff16dc0041084449e606d0fb3c/packages/image_picker/image_picker/lib/image_picker.dart#L203

If you want continue use PickedFile let change to getImage

CodePudding user response:

Chnage your declaration to:

XFile _imageFile; 

Instead of pickedfile. And remove the case in as PickedFile

CodePudding user response:

 final ImagePicker _picker = ImagePicker();

  Future<File?> takePhoto(ImageSource source) async {
    final XFile? image = await _picker.pickImage(source: source);                  
    
    final File? file = File(image!.path);
    return file;
  }

The issue is with using declaring it as a PickedFile initially.

  • Related