无法使用颤振库“image_picker”获取图像

时间:2021-05-15 08:53:38

标签: image flutter dart

我已将 flutter 中的 image_picker 库更新到最新版本,并且在执行以下代码时出现错误

代码

File file = await ImagePicker.getImage(
      source: ImageSource.camera,
      maxHeight: 675,
      maxWidth: 960,
    );

错误

error: Instance member 'getImage' can't be accessed using static access. 
error: A value of type 'PickedFile' can't be assigned to a variable of type 'File'.

请指导我解决这个问题

2 个答案:

答案 0 :(得分:4)

如错误消息中所述,ImagePicker.getImage() 返回一个 PickedFile

您可以像这样将其转换为 File 类型:

PickedFile pickedFile = await ImagePicker.getImage(
  source: ImageSource.camera,
  maxHeight: 675,
  maxWidth: 960,
);
            
File imageFile = File(pickedFile.path);

答案 1 :(得分:0)

安装图片选择器并正确设置

Imagepicker

初始化变量

File _image;
final Imagepicker = ImagePicker();

底部模型打开按钮

 ElevatedButton(
              style: ElevatedButton.styleFrom(
                primary: Colors.red, // background
                onPrimary: Colors.white, // foreground
              ),
              onPressed: () {
                openImagePickerModal(context);},
              child: Text('Select Image'),
            ),

查看所选图片

 Padding(
          padding: const EdgeInsets.all(8.0),
          child: Container(
              height: 200,
              width: 200,
              child: Image.file(
                _image,
                fit: BoxFit.fill,
              )),
        ),

图像选择器模型底部表,用于选择图像或捕获图像

  void openImagePickerModal(BuildContext context) {
    final flatButtonColor = Theme.of(context).primaryColor;
    showModalBottomSheet(
        context: context,
        builder: (BuildContext context) {
          return Container(
            height: 180.0,
            padding: EdgeInsets.all(20.0),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Text(
                  'Pick An Image',
                  style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
                ),
                SizedBox(
                  height: 15.0,
                ),
                FlatButton(
                  textColor: flatButtonColor,
                  child: Text(
                    'Use Camera',
                    style: TextStyle(fontSize: 15),
                  ),
                  onPressed: () {
                    Pickimage(context, ImageSource.camera);
                    Navigator.pop(context);
                  },
                ),
                FlatButton(
                  textColor: flatButtonColor,
                  child: Text(
                    'Use Gallery',
                    style: TextStyle(fontSize: 15),
                  ),
                  onPressed: () {
                    Pickimage(context, ImageSource.gallery);
                    Navigator.pop(context);
                  },
                ),
              ],
            ),
          );
        });
  }

从图库或相机图像中选择图像的功能

  Future Pickimage(BuildContext context, ImageSource source) async {
    if (source != null) {
      final pickedFile = await ImagePicker.pickImage(source: source);

      setState(() {
        if (pickedFile != null) {
          _image = File(pickedFile.path);
       
        } else {
          print('No image selected.');
        }
      });
    } else {}
  }