I'm getting The method 'copy' isn't defined for the type 'XFile' error when I try to copy my image to a specific location. error. When I try to copy an imaged to a specific location in the device
Detailed error : The method 'copy' isn't defined for the type 'XFile'. Try correcting the name to the name of an existing method, or defining a method named 'copy'.dartundefined_method
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart' as path;
import 'package:path_provider/path_provider.dart' as syspaths;
class ImageInput extends StatefulWidget {
@override
_ImageInputState createState() => _ImageInputState();
}
class _ImageInputState extends State<ImageInput> {
File _storedImage;
Future<void> _takePicture() async {
final picker = ImagePicker();
final imageFile = await picker.pickImage(
source: ImageSource.camera,
maxWidth: 600,
);
setState(() {
_storedImage = File(imageFile.path);
});
final appDir = await syspaths.getApplicationDocumentsDirectory();
final fileName = path.basename(imageFile.path);
final savedImage = await imageFile.copy('${appDir.path}/$fileName');
}
CodePudding user response:
Try below code hope its help to you.
class ImageInput extends StatefulWidget {
@override
_ImageInputState createState() => _ImageInputState();
}
class _ImageInputState extends State<ImageInput> {
File? storedImage;
Future<void> takePicture() async {
final picker = ImagePicker();
final imageFile = await picker.pickImage(
source: ImageSource.camera,
maxWidth: 600,
);
setState(() {
storedImage = File(imageFile!.path);
});
final appDir = await syspaths.getApplicationDocumentsDirectory();
final fileName = path.basename(imageFile!.path);
final savedImage = await File(imageFile.path).copy('${appDir.path}/$fileName');
}