没有为“PickedFile”类型定义方法“copy”

时间:2021-03-10 01:30:07

标签: flutter dart methods copy imagepicker

在我升级 Flutter 后,我按照迁移代码的所有步骤进行操作,现在出现此错误,无法使用 .Copy。

Firefox

我的地图也有问题,当我在没有 imageInput 类 onPressed 按钮的情况下进行调试以选择一个位置时,我的应用程序崩溃了。方法 'map' 在 null 上被调用。

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

我对 imagepicker 进行了如下更改,以便我可以调试并且看起来一切正常,但在控制台中我得到: 无法打开文件,路径 = '/data/user/0/com.example.flutter_complete_guide/app_flutter/scaled_5659b7a1-cc8d-4171-b0f1-69e40962c8893113442133536147308.jpg'(操作系统错误,无此类文件,无错误:强>

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

1 个答案:

答案 0 :(得分:0)

答案就在您的代码中。在 setState 中以本地状态存储文件时,您将文件类型从“PickedFile”转换为“File”。复制文件时执行相同操作。

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