Bisclavret
Bisclavret

Reputation: 1331

ImagePicker.pickImage(source: ImageSource.gallery) returns null

Steps to Reproduce Create a flutter app. Try and use the most basic functionality of the image_picker plugin.

Expected results:

When you select an image from the gallery, it should return something to your application. A file, or location. Anything really.

Actual results:

Nothing. The gallery launches. You select an image. Gallery goes away, nothing is returned to the app at all. Just leaves null.

My code

Future getImage() async {
  var image = await ImagePicker.pickImage(source: ImageSource.gallery);
  customImageFile = image.toString();
  print('customImageFile: ' + customImageFile);
}

Output from code

customImageFile: null

Plugin Version

image_picker: 0.6.5

Flutter doctor

[√] Flutter (Channel stable, v1.12.13+hotfix.5, on Microsoft Windows [Version 10.0.17763.1098], locale en-AU)
• Flutter version 1.12.13+hotfix.5 at c:\flutter
• Framework revision 27321eb (4 months ago), 2019-12-10 18:15:01 -0800
• Engine revision 2994f7e1e6
• Dart version 2.7.0

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Installations\AndroidSDK
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• ANDROID_HOME = C:\Installations\AndroidSDK
• Java binary at: C:\Installations\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
• All Android licenses accepted.

[√] Android Studio (version 3.5)
• Android Studio at C:\Installations\Android Studio
• Flutter plugin version 40.2.2
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] Connected device (1 available)
• SM G970F • android-arm64 • Android 10 (API 29)

• No issues found!

Upvotes: 1

Views: 11106

Answers (4)

Sarthak Bansal
Sarthak Bansal

Reputation: 124

The image_picker returns null on IOS simulator on M1 Macs. This is a problem that is still not resolved. Use a physical device. Your code seems correct.

Upvotes: 0

JM8
JM8

Reputation: 11

You should typecast image variable as File:

var image = await ImagePicker().getImage(source: ImageSource.gallery) as File;

Upvotes: 1

Isisco
Isisco

Reputation: 141

Future getImage() async {
  var image = await ImagePicker().getImage(source: ImageSource.gallery);
  customImageFile = File(image.path);
  print('customImageFile: ' + customImageFile);
}

Flutter 2

Upvotes: 2

Nitin0310
Nitin0310

Reputation: 23

flutter

you need to create future function. store the result in a variable of File bcz it will return the image Path

Future<File> pickImage(ImageSource source) async{
    File testImage = await ImagePicker.pickImage(source: source);
    setState(() {
      pickedImage = testImage;
    });
  }

Upvotes: -2

Related Questions