Flutter ImagePicker – 在onPressed中异步获取图像显示警告提示

huangapple go评论93阅读模式
英文:

Flutter ImagePicker - Getting image asynchronously in onPressed shows lint warning

问题

我正在实现一个使用ImagePicker上传个人资料图片的功能,并将逻辑放入按钮的onPressed函数中,代码如下:

OutlinedButton.icon(
  icon: Icon(Icons.upload),
  label: Text("选择个人资料图片"),
  onPressed: () async {
    XFile? image = await introVM.imagePicker.pickImage(
        source: ImageSource.gallery,
        imageQuality: 50,
        preferredCameraDevice: CameraDevice.front);
    if (image != null) introVM.setProfilePicture(image.path);
  },
);

一切都正常,没有错误,但我收到了一个关于async部分的lint警告:

预期是同步函数,但是得到了异步函数。

我做错了吗?

英文:

I am implementing a profile picture upload with ImagePicker and put the logic into the onPressed function of a button like this:

    OutlinedButton.icon(
      icon: Icon(Icons.upload),
      label: Text("Select profile picture"),
      onPressed: () async {
        XFile? image = await introVM.imagePicker.pickImage(
            source: ImageSource.gallery,
            imageQuality: 50,
            preferredCameraDevice: CameraDevice.front);
        if (image != null) introVM.setProfilePicture(image!.path);
      },
    );

Everything works fine without errors, but I am getting a lint warning about the async part:

> Expected a sync function but got async.

Am I doing it wrong?

答案1

得分: 2

根据Dart Code Metrics,这是对Dart代码设计的警告,该设计避免在期望同步调用的地方调用异步函数。

避免在同步期望时传递异步函数。

为了避免linting的投诉,目前有两种方法。

  1. 使用then()方法
OutlinedButton.icon(
    icon: Icon(Icons.upload),
    label: Text("选择个人资料图片"),
    onPressed: () {
        introVM.imagePicker.pickImage(
            source: ImageSource.gallery, 
            imageQuality: 50,
            preferredCameraDevice: CameraDevice.front
        ).then((XFile? xFile) {
             if (xFile != null) introVM.setProfilePicture(xFile!.path);
        });
    },
);
  1. 使用匿名函数(我不推荐这种方式,我们应该将其转换为单独的函数)
OutlinedButton.icon(
    icon: Icon(Icons.upload),
    label: Text("选择个人资料图片"),
    onPressed: () {
        // 应该移到单独的函数
        () async {
            XFile? image = await introVM.imagePicker.pickImage(
                source: ImageSource.gallery,
                imageQuality: 50,
                preferredCameraDevice: CameraDevice.front);
            if (image != null) introVM.setProfilePicture(image!.path);
         };
    },
);
英文:

According to Dart Code Metrics, it is a warning for a Dart code design that avoids calling an asynchronous function where a synchronous is expected.

avoid-passing-async-when-sync-expected

To avoid complaints from linting, there are 2 ways for now.

  1. use then() method
OutlinedButton.icon(
    icon: Icon(Icons.upload),
    label: Text("Select profile picture"),
    onPressed: () {
        introVM.imagePicker.pickImage(
            source: ImageSource.gallery, 
            imageQuality: 50,
            preferredCameraDevice: CameraDevice.front
        ).then((XFile? xFile) {
             if (xFile != null) introVM.setProfilePicture(xFile!.path);
        });
    },
);
  1. Use the anonymous function (I am not preferred that one, and we should convert it into a separate function)
OutlinedButton.icon(
    icon: Icon(Icons.upload),
    label: Text("Select profile picture"),
    onPressed: () {
        // Should move to a separate function
        () async {
            XFile? image = await introVM.imagePicker.pickImage(
                source: ImageSource.gallery,
                imageQuality: 50,
                preferredCameraDevice: CameraDevice.front);
            if (image != null) introVM.setProfilePicture(image!.path);
         };
    },
);

huangapple
  • 本文由 发表于 2023年5月22日 00:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定