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

1,288

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');
Share:
1,288
Admin
Author by

Admin

Updated on December 28, 2022

Comments

  • Admin
    Admin over 1 year

    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);