ImagePicker.platform 显示警告 - Flutter

ImagePicker.platform shows warning - Flutter

我正在使用以下代码从用户的图库中挑选图片。

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

我收到以下警告。

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

我不确定警告的含义。我尝试查找它,但无法找出解决此警告的解决方案。

试试下面的代码希望对你有帮助

声明文件类型形式dart.io

File? imagePicked;

创建拾取图像的函数

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

创建您的小部件

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

您只需更改代码即可

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

ImagePicker().pickImage(...)

所以

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