方法 getImage 未定义

Tam*_*bul 2 dart flutter imagepicker

我正在使用image_picker 0.6.7+17库以便使用手机摄像头拍摄图像。
我使用的是 android 设备而不是 ios 设备。

一个问题

似乎getImage没有定义该方法,我从文档中获取了这个确切的代码:

  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);
  }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

lib/pickers/image_picker.dart:17:37: Error: The method 'getImage' isn't defined for the class 
'ImagePicker'.
 - 'ImagePicker' is from 'package:chat_app/pickers/image_picker.dart' 
('lib/pickers/image_picker.dart').
Try correcting the name to the name of an existing method, or defining a method named 'getImage'.
final pickedFile = await picker.getImage(source: ImageSource.camera);
                                ^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

到目前为止我做了什么:

  • 将依赖项添加到我的 pubspec.yaml: 文件
dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.0
  cloud_firestore: 0.13.5
  firebase_auth: 0.16.1
  image_picker: ^0.6.7+17
Run Code Online (Sandbox Code Playgroud)
  • 添加android:requestLegacyExternalStorage="true"AndroidManifest.xml文件中

  • 还导入import 'package:image_picker/image_picker.dart'以使用此库


可能是什么问题呢?

小智 5

您正在尝试使用带有指定使用新 API 的插件版本的旧 API。旧 API

File image = await ImagePicker.pickImage(...)
Run Code Online (Sandbox Code Playgroud)

新API

final _picker = ImagePicker();
.
.
.
PickedFile image = await _picker.getImage(...)
Run Code Online (Sandbox Code Playgroud)