'ImagePicker.platform shows warning - Flutter

I am using the following code to pick an image from user's gallery.

Future getImageFromGallery(BuildContext context) async {
    await ImagePicker.platform()
        .pickImage(source: ImageSource.gallery)
        .then((image) {
      if (image != null) {
        _cropImage(image, context);
      }
    });
  }

I am getting the following warning.

The member 'platform' can only be used within 'package:image_picker/image_picker.dart' or a test.

I'm not sure what the warning means. I tried looking it up but couldn't figure out the solution to resolve this warning.



Solution 1:[1]

You can just change the code

ImagePicker.platform().pickImage(...)

to

ImagePicker().pickImage(...)

so

Future getImageFromGallery(BuildContext context) async {
    await ImagePicker()
      .pickImage(source: ImageSource.gallery)
      .then((image) {
      if (image != null) {
         _cropImage(image, context);
      }
   });
}

Solution 2:[2]

Try below code hope its help to you

Declare File type form dart.io package

File? imagePicked;

Create Function for pick up the image

void gallaryImage() async {
    final picker = ImagePicker();
    final pickedImage = await picker.pickImage(
      source: ImageSource.gallery,
    );
    final pickedImageFile = File(pickedImage!.path);
    setState(() {
      imagePicked = pickedImageFile;
    });
  }

Create your Widget

TextButton(
      onPressed: gallaryImage,
      child: Text(
      'Gallery',
      style: TextStyle(
          color: Colors.black,
       ),
     ),
    ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gilbriatore
Solution 2 Ravindra S. Patil