The method getImage isn't defined

Viewed 5248

I am using image_picker 0.6.7+17 library in order to take an image using the phone camera.
I am using an android device and not an ios device.

A problem

It seems like that getImage method is not defined, I took this exact code from the docs:

  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);
  }

I am getting this error:

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

What I have done so far:

  • Added the dependency to my pubspec.yaml: file
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
  • Added android:requestLegacyExternalStorage="true" to the AndroidManifest.xml file

  • Also Imported import 'package:image_picker/image_picker.dart' to use this library


What could be the problem?

5 Answers

You are trying to use an old API with a plugin version that specifies to use the new API. Old API

File image = await ImagePicker.pickImage(...)

New API

final _picker = ImagePicker();
.
.
.
PickedFile image = await _picker.getImage(...)

Are you sure the ImagePicker you are using is not the one from this package:chat_app/pickers/image_picker.dart ? Maybe there is a class name conflict and you must rename your own ImagePicker class

I got the same problem I solved by defining the picker like that

 final picker = ImagePicker();

and then use this

    Future<void> _chooseImage() async {
  var pickedFile = await picker.getImage(source: ImageSource.gallery);
}

1)you may forget import the library of "image picker":import 'package:image_picker/image_picker.dart';

2)or import wrong library with the same name with some differences in the syntax of Sentence

Just upgrade to latest version of image_picker. And replace getImage by pickImage bcz getImage has deprecated.

Related