Reputation: 618
I am trying to select image from gallery with imagepicker and upload it to firebasestorage. When the application runs, it gives the error "Cannot open file, path = '' (OS Error: Bad address, errno = 14) and Unhandled Exception: type 'XFile' is not a subtype of type 'File' in type cast'.
GestureDetector(
onTap: () => pickPhotoFromGallery(),
child: CircleAvatar(
radius: 70,
// ignore: unnecessary_null_comparison
child: (_imageFile != null)
? Image.file(_imageFile)
: Image.asset('assets/images/icon.png'),
),
),
Future pickPhotoFromGallery() async {
File imageFile =
(await _imagePicker.pickImage(source: ImageSource.gallery)) as File;
setState(() {
_imageFile = imageFile;
});
}
When I click the save button, it says "Failed assertion: line 127 pos 12: 'file.absolute.existsSync()': is not true." I am encountering the error.
onPressed: uploading ? null : () => uploadImageAndSaveItemInfo(),
uploadImageAndSaveItemInfo() async {
setState(() {
uploading = true;
});
String imageDownloadUrl = await uploadItemImage(_imageFile);
saveItemInfo(imageDownloadUrl);
saveItemInfoCategory(imageDownloadUrl);
}
Future<String> uploadItemImage(File mFileImage) async {
final Reference storageReference =
FirebaseStorage.instance.ref().child("thumbnail");
UploadTask uploadTask = storageReference
.child(_idController.text.trim() + ".jpg")
.putFile(mFileImage);
String downloadUrl = await uploadTask.snapshot.ref.getDownloadURL();
return downloadUrl;
}
Imagepicker was working in old versions, it gives an error in the latest version.
Upvotes: 4
Views: 8169
Reputation: 196
The reason for this is that the image picker was upgraded to use Xfile instead of File. To convert Xfile to File you can use:
File file = File( _imageFile.path );
You will need to add import 'dart:io'
as well.
Upvotes: 18