【问题标题】:Not able to get image using flutter library "image_picker"无法使用颤振库“image_picker”获取图像
【发布时间】:2021-05-15 08:53:38
【问题描述】:

我已将 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'.

请指导我解决这个问题

【问题讨论】:

  • 能不能把完整的代码文件也加进去?

标签: image flutter dart


【解决方案1】:

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

你可以把它转换成File这样的类型:

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

【讨论】:

  • 这个工作,而不是使用文件文件,使用PickedFilepickfile,然后在挑选图像文件后将pickedfile路径分配给文件对象
【解决方案2】:

正确安装图像选择器和设置

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 {}
  }

【讨论】:

    【解决方案3】:

    如果您想使用上述代码,则必须更改包版本

    更改 pubspec.yaml 文件中的依赖项

     image_picker: ^0.6.0
    

    【讨论】:

      猜你喜欢
      • 2021-01-06
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 2020-01-13
      • 2021-12-22
      • 1970-01-01
      相关资源
      最近更新 更多