In this example, we are going to show you how to solve the error "type 'XFile' is not a subtype of type 'File'" error in Flutter App. We will also describe the cause of the error.
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═════════════ The following _TypeError was thrown building Home(dirty, state: _HomeState#361f6): type 'XFile' is not a subtype of type 'File'
This error happens when you use XFile as a File in your app. For example:
Image.file(image) //runtime error: type 'XFile' is not a subtype of type 'File'
To solve this error, you can convert the XFile to File using the following Code:
File? file = File(XFile!.path);
For Example: (From image_picker plugin)
final ImagePicker _picker = ImagePicker(); //pick file from gallery, it will return XFile final XFile? image = await _picker.pickImage(source: ImageSource.gallery); //convert XFile to File final File? imagefile = File(image!.path);
Now you can use this in Image() Widget:
Or, you can also directly set Xfile to Image Widget like below:
In this way, you can solve "type 'XFile' is not a subtype of type 'File'" error in your Flutter App.
No any Comments on this Article