英文:
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的投诉,目前有两种方法。
- 使用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);
        });
    },
);
- 使用匿名函数(我不推荐这种方式,我们应该将其转换为单独的函数)
 
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.
- 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);
        });
    },
);
- 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);
         };
    },
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论