如何在flutter中使用image_picker

2023-12-30

我对如何使用 image_picker 感到困惑,这就是我在应用程序中使用它的方式(就像在许多教程中一样):

class AddDialogState extends State<AddDialog> {
    File galleryFile;
    Widget _onlyStatus() {
         getLocalImage() async {
               var _galleryFile = await ImagePicker.pickImage(
                     source: ImageSource.gallery
               };
               setState(() {
                   galleryFile = _galleryFile;
               });
               print(_galleryFile.path);
         }

         return Column(
               ........
               FlatButton.icon(
                    onPressed: () {
                         getLocalImage();
                    }
               )
         )

    }

    @override
    Widget build(BuildContext context) {
         // fullscreen dialog
         .........
         body: _onlyStatus()
    }
}

问题是,上面的代码没有启动 ImagePicker,当我单击 FlatButton 时,上面的代码只会产生错误the getter 'path' was called on null,它不会启动任何与画廊相关的新活动,那么我的代码有什么问题吗?


实际上 pickImage() 现在已被弃用。所以你必须使用 ImagePicker.getImage(来源: ImageSource.gallery)

点击这里了解更多 https://androidride.com/flutter-notes-taking-app-tutorial/

void getImage(ImageSource imageSource) async {
    PickedFile imageFile = await picker.getImage(source: imageSource);
    if (imageFile == null) return;
    File tmpFile = File(imageFile.path);
    final appDir = await getApplicationDocumentsDirectory();
    final fileName = basename(imageFile.path);
    tmpFile = await tmpFile.copy('${appDir.path}/$fileName');
    setState(() {
      _image = tmpFile;
    
    });
  }

该代码还将图像文件存储在设备目录中。还使用路径包。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在flutter中使用image_picker 的相关文章

随机推荐