ImagePicker.platform 显示警告 - Flutter

pra*_*179 4 compiler-warnings flutter imagepicker

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

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

我收到以下警告。

The member 'platform' can only be used within 'package:image_picker/image_picker.dart' or a test.
Run Code Online (Sandbox Code Playgroud)

我不确定这个警告是什么意思。我尝试查找它,但无法找到解决此警告的解决方案。

Rav*_*til 5

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

声明文件类型表单dart.io

File? imagePicked;
Run Code Online (Sandbox Code Playgroud)

创建拾取图像的函数

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

创建您的小部件

TextButton(
      onPressed: gallaryImage,
      child: Text(
      'Gallery',
      style: TextStyle(
          color: Colors.black,
       ),
     ),
    ),
Run Code Online (Sandbox Code Playgroud)