The method getImage isn't defined
Asked Answered
C

7

5

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?

Catgut answered 21/12, 2020 at 21:38 Comment(0)
S
3

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

Suspect answered 21/12, 2020 at 21:50 Comment(0)
Z
8

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(...)
Ziegler answered 13/6, 2021 at 13:28 Comment(0)
S
3

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

Suspect answered 21/12, 2020 at 21:50 Comment(0)
Q
3

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

Quinquefid answered 20/7, 2021 at 5:42 Comment(0)
S
2

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);
}
Suet answered 25/7, 2021 at 8:38 Comment(0)
O
0

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

Objectify answered 30/7, 2022 at 19:27 Comment(0)
W
0

I used pickImage instead of getImage - worked pretty well for me.

import 'package:image_picker/image_picker.dart';

// some code

final ImagePicker _picker = ImagePicker();


  Future<void> _pickImage() async {
    final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
    if (pickedFile != null) {
      setState(() {
        _imagePath = pickedFile.path;
      });
    }
  }
Work answered 7/9 at 4:51 Comment(0)
M
-1
  Future<void> imagePicker() async {
    ImagePicker picker = ImagePicker();
    XFile? xFile = await picker.pickImage(source: ImageSource.gallery);
    if (xFile != null) {
      setState(() {
        imgFile = File(xFile.path);
      });
    }
  }
Mccutchen answered 27/8 at 6:29 Comment(1)
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Cathode

© 2022 - 2024 — McMap. All rights reserved.