首页 > 解决方案 > 无法使用图像选择器颤振

问题描述

我是 Flutter 的新手,我用它做了一个项目,我有一个问题,
当我想使用 imagepicker 时,它无法使用,我在 youtube 上一步一步地进行操作,我收到错误

“文件 imageFile;表示必须初始化不可为空的实例字段‘imageFile’。”

并在“Pickedfile.path 表示不能无条件访问属性‘路径’,因为接收器可以是‘空’。”

这张显示错误的图片

这是我写的代码

  File imageFile;
  final picker = ImagePicker();

  chooseImage(ImageSource source) async {
    final PickedFile = await picker.pickImage(source: source);

    setState(() {
      imageFile = File(PickedFile.path);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          onPressed: () {},
          icon: Icon(Icons.exit_to_app),
        ),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Container(
                  child: imageFile != null
                      ? Container(
                          height: 120.0,
                          width: 120.0,
                          decoration: BoxDecoration(
                              image: DecorationImage(
                            image: FileImage(imageFile),
                          )),
                        )
                      : Container(
                          height: 120.0,
                          width: 120.0,
                          decoration: BoxDecoration(color: Colors.blue),
                        )),
              Container(
                child: Padding(
                  padding: EdgeInsets.fromLTRB(0, 0, 0, 0),
                  child: ElevatedButton(
                    onPressed: () {
                      chooseImage(ImageSource.gallery)
                    },
                    child: Text('Ubah Foto Profil'),
                  ),
                ),
              ),

标签: androidflutterdart

解决方案


请让我知道这对你有没有用。

  ImagePicker picker = ImagePicker();
  var imageFile;
 _getFromGallery() async {
var pickedFile = (await picker.pickImage(
  source: ImageSource.gallery,
));
if (pickedFile != null) {
  setState(() {
    imageFile = File(pickedFile.path);
  });
}
}
Widget _imageSection() {
return (imageFile == null)
    ? Container(
  width:
  MediaQuery.of(context).size.width,
  height: 220,
  child: Card(
    elevation: 3.0,
    color: Colors.white,
    shadowColor: Colors.grey,
    // child: Image.asset(ImageUtil
    //     .PUNCH_IMAGE_PLACEHOLDER),
    child: Text("No image selected"),
  ),
)
    : Container(
  /*decoration: BoxDecoration(
    border: Border.all(color: 
ColorUtil.leavePageContainerBorder),
  ),*/
  width:
  MediaQuery.of(context).size.width,
  height: 220,
  child: Column(
    children: [
      // Text(imageFile),
      Image.file(imageFile,
        fit: BoxFit.cover,
      ),
    ],
  ),
);
}

  @override
 Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(
    leading: IconButton(
      onPressed: () {},
      icon: const Icon(Icons.exit_to_app),
    ),
  ),
  body: SingleChildScrollView(
    child: Center(
      child: Column(
          mainAxisAlignment:
              MainAxisAlignment.start,
          children: <Widget>[
            Container(
              child: Padding(
                padding: const EdgeInsets.fromLTRB(
                    0, 0, 0, 0),
                child: ElevatedButton(
                  onPressed: () {
                    _getFromGallery();
                    print(imageFile);
                  },
                  child: Text(
                      'Ubah Foto Profil'),
                ),
              ),
            ),
            _imageSection(),
          ]),
    ),
  ),
);
}

推荐阅读