'The method 'copy' isn't defined for the type 'PickedFile'

After i upgraded Flutter i followed all the steps for migration code and now i get this error, can't use to .Copy.

class ImageInput extends StatefulWidget {
  final Function onSelectImage;

  ImageInput(this.onSelectImage);

  @override
  _ImageInputState createState() => _ImageInputState();
}

class _ImageInputState extends State<ImageInput> {
  File _storedImage;
  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile =
        await picker.getImage(source: ImageSource.camera, maxWidth: 600);
    setState(() {
      if (pickedFile != null) {
        _storedImage = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });
    final appDir = await syspaths.getApplicationDocumentsDirectory();
    final fileName = path.basename(pickedFile.path);
    final savedImage = await pickedFile.copy('${appDir.path}/$fileName');
    widget.onSelectImage(savedImage);
  }

I get as well an issue with my maps, when i debug without imageInput class onPressed button for select a location i get crashed my app. The method 'map' was called on null.

class _MapScreenState extends State<MapScreen> {
  LatLng _pickedLocation;

  void _selectLocation(LatLng position) {
    setState(() {
      _pickedLocation = position;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Your Map'),
        actions: <Widget>[
          if (widget.isSelecting)
            IconButton(
              icon: Icon(Icons.check),
              onPressed: _pickedLocation == null
                  ? null
                  : () {
                      Navigator.of(context).pop(_pickedLocation);
                    },
            ),
        ],
      ),
      body: GoogleMap(
        initialCameraPosition: CameraPosition(
          target: LatLng(
            widget.initialLocation.latitude,
            widget.initialLocation.longitude,
          ),
          zoom: 16,
        ),
        onTap: widget.isSelecting ? _selectLocation : null,
        markers: (_pickedLocation == null && widget.isSelecting)
            ? null
            : {
                Marker(
                  markerId: MarkerId('m1'),
                  position: _pickedLocation ??
                      LatLng(
                        widget.initialLocation.latitude,
                        widget.initialLocation.longitude,
                      ),
                ),
              },
      ),
    );
  }
}

I made this changes you see below for imagepicker so i can debug and seems everything ok, but in console i get: Cannot open file, path = '/data/user/0/com.example.flutter_complete_guide/app_flutter/scaled_5659b7a1-cc8d-4171-b0f1-69e40962c8893113442133536147308.jpg' (OS Error: No such file or directory, errno = 2)

final appDir = await syspaths.getApplicationDocumentsDirectory();
final fileName = path.basename(pickedFile.path);
final savedImage = await _storedImage.copy('${appDir.path}/$fileName');
widget.onSelectImage(savedImage);


Solution 1:[1]

The answer is in your code itself. While storing the file in the local state in setState, you converted the file type from 'PickedFile' to 'File'. Do the same while copying the file.

final savedImage = await File(imageFile.path).copy('${appDir.path}/$fileName');

Solution 2:[2]

The issue here is that the method copy() is not defined (ie available) for a PickedFile. Note that File objects have copy() available on them though. So on line 12, instead of having imageFile.copy() you should replace it with _storedImage.copy() instead, which should resolve your issue.

Solution 3:[3]

your pickedFile does not have the copy method, do this

final savedImage = await _storedImage!.copy('${appDir.path}/$fileName');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 pltc
Solution 3 teddy 225